EJB injection into Restful service (NullPointerException)

柔情痞子 提交于 2020-01-03 04:14:10

问题


I' have a problem with injecting StoreBean into Restful service. I have reduecd code little bit to get a point.

Here is the code(DataStoreBean):

@Local(DataStore.class)
@Stateless
public class DataStoreBean implements DataStore
{
     public String get()
     {
        return "works";
     }
}

and here is code for RestEndpoint:

@RequestScoped
@Path("/DataStore")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class DataStoreRestEndpoint
{

@EJB
DataStoreBean dataStore;


    @GET
    @Path("/test")
    public String get()
    {
       return dataStore.get();
    }

 }

When I executed I always get a NPE.

     Caused by: java.lang.NullPointerException
   at org.it.datastore.DataStoreRestEndpoint.get(DataStoreRestEndpoint.java:112) [classes:]
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_25]
   at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) [rt.jar:1.7.0_25]
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) [rt.jar:1.7.0_25]
   at java.lang.reflect.Method.invoke(Unknown Source) [rt.jar:1.7.0_25]
   at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:155) [resteasy-jaxrs-2.3.1.GA.jar:]
   at org.jboss.resteasy.core.ResourceMethod.invokeOnTarget(ResourceMethod.java:257) [resteasy-jaxrs-2.3.1.GA.jar:]
   at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:222) [resteasy-jaxrs-2.3.1.GA.jar:]
   at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:211) [resteasy-jaxrs-2.3.1.GA.jar:]
   at org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:525) [resteasy-jaxrs-2.3.1.GA.jar:]
... 19 more

I'm using JBoss 7.1 and EJB3


回答1:


Declare the restful class with @Stateless, too. Here is an example: Inject an EJB into JAX-RS (RESTful service)



来源:https://stackoverflow.com/questions/22022019/ejb-injection-into-restful-service-nullpointerexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!