Reputation: 7633
I thought, for a class to be inject-able, any class has to be annotated. But I am seeing one example demonstrating a simple REST service, where a class without any annotation is injected. HelloService isn't annotated. Is it a Bean? What about its scope of life cycle?
/**
* A simple CDI service which is able to say hello to someone
*
* @author Pete Muir
*
*/
public class HelloService {
String createHelloMessage(String name) {
return "Hello " + name + "!";
}
}
/**
* A simple REST service which is able to say hello to someone using HelloService Please take a look at the web.xml where JAX-RS
* is enabled And notice the @PathParam which expects the URL to contain /json/David or /xml/Mary
*
* @author [email protected]
*/
@Path("/")
public class HelloWorld {
@Inject
HelloService helloService;
@POST
@Path("/json/{name}")
@Produces("application/json")
public String getHelloWorldJSON(@PathParam("name") String name) {
System.out.println("name: " + name);
return "{\"result\":\"" + helloService.createHelloMessage(name) + "\"}";
}
@POST
@Path("/xml/{name}")
@Produces("application/xml")
public String getHelloWorldXML(@PathParam("name") String name) {
System.out.println("name: " + name);
return "<xml><result>" + helloService.createHelloMessage(name) + "</result></xml>";
}
}
One more question in this example is that, why is it using "hello" in web.xml? There is no any class defined called "hello", and there is no any directory named as "hello" in the project.
<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">
<!-- One of the ways of activating REST Servises is adding these lines.
The server is responsible for adding the corresponding servlet automatically.
The class org.jboss.as.quickstarts.html5rest.HelloWorld class has the
annotation @Path("/") to receive the REST invocation -->
<servlet-mapping>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
<url-pattern>/hello/*</url-pattern>
</servlet-mapping>
</web-app>
Upvotes: 1
Views: 657
Reputation: 51
For a REST service bean to be able to inject other services, you should use CDI beans or EJB beans as a REST service implementation bean. If you don't specify the type of REST implementation bean, it depends on provider, what kind of implementation will be used. Usually it is not injectable by default implementation.
Generally in most of the cases either @Named for CDI bean implementation and @Stateless for EJB implementation is used.
Upvotes: 1
Reputation: 2893
You did not mention whether this is Jave EE 6 or 7, and you also did not include the beans.xml
file.
In Java EE 6, any class with a public default constructor can be injected and then automatically becomes a managed CDI bean, if there is a META-INF/beans.xml
. In Java EE 7, the same can be achieved with bean-discovery-mode="all"
in beans.xml
, see this question
Upvotes: 3