0

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!

Jimmy
  • 2,589
  • 21
  • 31
Vikram
  • 15
  • 1
  • 10
  • Do you have a `@Path` annotation on your class which extends Application? – dgebert Jan 02 '19 at 17:03
  • Since I am providing the Application related mapping in web.xml, I am not sure if we need the ApplicationPath annotation on the class which is extending again. The Restful Service is getting called fine, except throws a NullPointer while initiating the class under @EJB annotation. – Vikram Jan 02 '19 at 17:21
  • what is the value of version in web.xml root ? – Jimmy Jan 02 '19 at 17:34
  • @Jimmy : – Vikram Jan 02 '19 at 18:15
  • I think the version in use is an issue. The web app version should probably be 3.0 or 3.1 in order to use the appropriate level of JAX-RS support. What WAS version are you using? The tag says WAS 8 - if that is the case, there is no support of JAX-RS resources as EJBs (and it is out of support). If you are using WAS 8.5.5 or 9, it would be worth updating the web-app version. – Andy McCright Jan 03 '19 at 02:57
  • I am using WAS 8.5.5 and had tried with version 3.0 but no success. – Vikram Jan 03 '19 at 20:16
  • You might be using the JAX-RS 2.0 approach to JAX-RS/EJB integration. From this page ( https://www.ibm.com/support/knowledgecenter/en/SSAW57_8.5.5/com.ibm.websphere.nd.multiplatform.doc/ae/twbs_jaxrs_ejb_localinterface.html ), it looks like you should put your JAX-RS annotations on the EJB local interface rather than the bean class directly. IIRC, in WAS v9 and Liberty, your approach of putting the JAX-RS annotations on the bean class should work. Can you try putting the `@Path`, `@POST`, `@Consumes`, etc annotations on the local interface? HTH – Andy McCright Jan 03 '19 at 23:29
  • I was referring to [this](https://www.ibm.com/support/knowledgecenter/en/SSAW57_8.5.5/com.ibm.websphere.nd.multiplatform.doc/ae/twbs_jaxrs_ejb_nointerface.html) article which targets the no-interface implementation. Make me wonder which one I should be using. – Vikram Jan 04 '19 at 20:57
  • Are you saying WAS 8.5.5 does not support the JAX-RS annotations on bean class and that's the reason @EJB will not work and I would get null reference to my object? Thanks! – Vikram Jan 04 '19 at 21:03
  • @Vikram, yes, that's my guess. The article you listed shows a no-interface view of the EJB - so in essence, the annotations on the bean class are on the EJB interface. But in your code, you are using a separate Local interface for the EJB. And IIUC, WAS 8.5.5 is expecting the annotations to be on the interface. Can you try moving the `@Path`, `@POST`, and `@Consumes` annotations from the bean class to the `IMyFacade` interface and see if that works? Alternatively, you could try removing the `IMyFacade` interface entirely and adding the `@LocalBean` annotation to the `MyFacade` bean class. – Andy McCright Jan 07 '19 at 16:54

0 Answers0