Reputation: 884
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
Upvotes: 1
Views: 4661
Reputation: 2095
Declare the restful class with @Stateless, too. Here is an example: Inject an EJB into JAX-RS (RESTful service)
Upvotes: 3