Вопрос

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

Это было полезно?

Решение 2

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.

Другие советы

Regarding the casting problem: you should cast to iface.HelloInterface and not to HelloImpl since your bean is wrapped with proxy.

I don't know why @EJB does not work, but I'm pretty sure that Wink has nothing to do with the EJB annotations...

I had similar problems with injection of EJBs (NPExc.) in app. deployed on WAS 8 (z/OS). I was using no interface view (local EJB in the same EAR). On local WAS8 everything worked fine. The solution was just removing EJBclient.jar from the WEB module class path (in manifest file).

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top