ejb-3.0jax-rscdiapache-winkwebsphere-8

@EJB injection not working from JAX-RS service on Websphere AS 8


I have a RESTful service which injects an EJB(3.0) using the @EJB annotation. This injection does not work as I get a NullPointerException when I try access the greeting() method on the bean.

This exact code works on Glassfish 3.1 and now on WAS8.0.2 it fails. The injection however works in the same application when referenced from a servlet using the exact same approach

@Stateless
@Path("/hello")
public class HelloRestService {

@EJB
public HelloInterface helloImpl;

}

My Bean looks like this

package impl;

@Stateless
@Local
public class HelloImpl implements iface.HelloInterface {

@Override
public String greeting() {

    return "Hello developer";
}

}

I have tried to do a lookup for the Implementation using the jndi name that gets printed out during server startup

helloimpl = (HelloImpl) new InitialContext().lookup("java:global/REST_EAR/REST_WAR/HelloImpl!iface.HelloInterface");

this however causes a ClassCastException

java.lang.ClassCastException: iface.EJSLocal0SLHelloImpl_f8ca883b incompatible with impl.HelloImpl

Can I read much into this..?

Now I am currently using Wink as my JAX-RS 1.1 implementation. I had previously used Jersey with the same results.

Does anyone know if this is a JAX-RS / WAS issue causing the DI to fail..? It definitely has something to do with REST as said previously the @EJB injection works from a Servlet


Solution

  • I managed to solve the issue in question, but was unable to use any other JAX-RS implementation than the default one in WAS 8. I had to extend javax.ws.rs.core.Application from every RESTful resource and in my web.xml I registered my servlet as

    <servlet>
        <servlet-name>javax.ws.rs.core.Application</servlet-name>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>javax.ws.rs.core.Application</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
    

    I also had to have the ibm-web-bnd.xml and the ibm-web.ext.xml in my WEB-INF folder in the web project. I have a empty beans.xml file in there too, this is required for CDI, but I cannot recall if this was essential.

    I hope this helps people sort out this issue as I have seen many online that cannot solve this.