I went though a few question related to this on Stackoverflow and implemented my code the same way but still the @EJB annotation is not working for me when I use it from Restful Services. 1. EJB injection into Restful service (NullPointerException) 2. Cant inject Bean class into Restfull WebService (JAX-RS)
I am using websphere application server to deploy the EAR and calling the rest service from a simple main class. In below code, I am expecting the myService to initialize but instead it throws a NullPointerException.
Below is my sample Resource class:
@Path(value = "/addresses")
@Stateless(name = "MyFacade")
public class MyFacade implements IMyFacade {
@EJB
private IMyService myService;
@Override
@POST
@Path("/postReq")
@Consumes("application/json")
public Response myMethod(RequestDTO requestDTO) {
Long docId = myService.generate(requestDTO);
return Response.status(201).entity(docId).build();
}
}
Interface:
@Local
public interface IMyFacade {
public Response myMethod(RequestDTO requestDTO);
}
Application extension:
public class UtilitiesApplication extends Application {
@Override
public Set<Class< ? >> getClasses() {
Set<Class< ? >> classes = new HashSet<Class< ? >>();
classes.add(MyFacade.class);
return classes;
}
}
Service class:
@Stateless
public class MyService implements IMyService {
//Implementation of generate() method.
}
Web.xml Entry
<servlet>
<description>
JAX-RS Tools Generated - Do not modify</description>
<servlet-name>my.facades.MyFacade</servlet-name>
<servlet-class>com.ibm.websphere.jaxrs.server.IBMRestServlet</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>my.facades.UtilitiesApplication</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>my.facades.MyFacade</servlet-name>
<url-pattern>/MyWS/*</url-pattern>
</servlet-mapping>
Please help me understand what I may be doing wrong. Let me know if any other information is needed. Thanks!