I was working on a RESTful service with JAX RS on Glassfish and pieced together a working implementation with CDI dependency injection.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import javax.annotation.ManagedBean; | |
| import javax.inject.Inject; | |
| import javax.ws.rs.GET; | |
| import javax.ws.rs.Path; | |
| import javax.ws.rs.Produces; | |
| import javax.ws.rs.QueryParam; | |
| import javax.xml.bind.annotation.XmlRootElement; | |
| /** | |
| * JAX RS implementation of REST logfile service. | |
| * @author Koert Zeilstra | |
| */ | |
| @Path("/test") | |
| @ManagedBean | |
| public class TestService { | |
| @Inject | |
| private HelloRepository helloRepository; | |
| @GET | |
| @Path("/hello") | |
| @Produces({"application/xml", "application/json"}) | |
| public Greeting test(@QueryParam(value = "name") String name) { | |
| Greeting greeting = new Greeting(); | |
| greeting.message = helloRepository.getGreeting(name); | |
| return greeting; | |
| } | |
| @XmlRootElement(name = "greeting") | |
| public static class Greeting { | |
| public String message; | |
| } | |
| } |
This REST service is pretty simple, when you access /test/hello?name=John with a GET, you will get “Hello world John” back. Note the @ManagedBean annotation – this will make CDI dependency injection working in a JAX RS instantiated service.
The HelloRepository is injected with CDI:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Example repository. | |
| * @author Koert Zeilstra | |
| */ | |
| public class HelloRepository { | |
| /** | |
| * @return Example greeting. | |
| */ | |
| public String getGreeting(String name) { | |
| return "Hello world " + name; | |
| } | |
| } |
You can package this as a WAR file and deploy it on a Glassfish server. To make this work, you need a minimal web.xml in WEB-INF:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="UTF-8"?> | |
| <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" | |
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
| xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> | |
| <display-name>test-service</display-name> | |
| </web-app> |
To enable CDI, you need an empty beans.xml in WEB-INF:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <beans xmlns="http://java.sun.com/xml/ns/javaee" | |
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
| xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd"> | |
| </beans> |
And to make JAX RS working correctly and set the root path, you need this class:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import javax.ws.rs.ApplicationPath; | |
| import javax.ws.rs.core.Application; | |
| /** | |
| * Application configuration. | |
| * See https://stackoverflow.com/questions/23508159/jax-rs-glassfish-eclipse-a-simple-web-service-doesnt-work | |
| * @author Koert Zeilstra | |
| */ | |
| @ApplicationPath("/") | |
| public class TestServiceApplication extends Application { | |
| } |
To set the context root, you could also add a glassfish-web.xml in WEB-INF – this is of course Glassfish specific.
After figuring this out, it is easy to configure and doesn’t need any complicated XML. I got the above configuration working on Glassfish 3 and 4.