简体   繁体   中英

EJB injection into Restful service (NullPointerException)

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM