2014-02-25 79 views
1

我在注入StoreBean到Restful服務時遇到問題。我已經重新編寫了一些代碼以獲得一個觀點。EJB注入Restful服務(NullPointerException)

下面是代碼(DataStoreBean):

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

這裏是代碼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(); 
    } 

} 

當我執行我總是得到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 

我使用JBoss 7.1和EJB3

回答

3

宣告以@Stateless在寧靜的班了。這裏有一個例子:Inject an EJB into JAX-RS (RESTful service)

+0

我試過,但再部署,當我得到這個錯誤:'不與類型的接口中發現EJB「org.it.datastore.DataStoreBean」的結合org.it.datastore.DataStoreRestEndpoint/dataStore' –

+0

你是否刪除了requestscope註釋? – pL4Gu33

+0

是的,我做到了。但仍然山姆錯誤。 –