1

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

Mitja Rogl
  • 894
  • 9
  • 22
  • 39

1 Answers1

3

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

Community
  • 1
  • 1
pL4Gu33
  • 2,045
  • 16
  • 38
  • I tried but then I get this error when deploying: `No EJB found with interface of type 'org.it.datastore.DataStoreBean' for binding org.it.datastore.DataStoreRestEndpoint/dataStore` – Mitja Rogl Feb 25 '14 at 17:52
  • Had you delete the requestscope annotation ? – pL4Gu33 Feb 25 '14 at 17:55
  • ohh did not see the class "DataStoreBean " had you add this in edit? For testing change DataStoreBean in restful service to DataStore. For me that works, for you, too ? – pL4Gu33 Feb 25 '14 at 18:08
  • If you want to use DataStoreBean add `@LocalBean` there and remove `@Local`. – pL4Gu33 Feb 25 '14 at 18:14
  • I did like you said and it is working. But now I have two classes annotated with ´@Stateless´. Is this ok? – Mitja Rogl Feb 25 '14 at 18:28
  • Yes both are annotated with ´@Stateless´ – Mitja Rogl Feb 25 '14 at 18:29