【问题标题】:EJB injection into Restful service (NullPointerException)EJB 注入 Restful 服务 (NullPointerException)
【发布时间】:2014-02-25 17:44:00
【问题描述】:

我在将 StoreBean 注入 Restful 服务时遇到问题。我已经减少了一点代码来说明问题。

这里是代码(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

【问题讨论】:

    标签: java dependency-injection ejb jboss7.x


    【解决方案1】:

    也用@Stateless 声明restful 类。这是一个例子:Inject an EJB into JAX-RS (RESTful service)

    【讨论】:

    • 我试过了,但部署时出现此错误:No EJB found with interface of type 'org.it.datastore.DataStoreBean' for binding org.it.datastore.DataStoreRestEndpoint/dataStore
    • 你删除了requestscope注解吗?
    • 哦,没有看到类“DataStoreBean”你在编辑中添加了这个吗?用于测试将 Restful 服务中的 DataStoreBean 更改为 DataStore。对我有用,对你也有用?
    • 如果你想使用 DataStoreBean 添加 @LocalBean 并删除 @Local
    • 我确实像你说的那样工作了。但是现在我有两个用'@Stateless'注释的类。这样可以吗?
    猜你喜欢
    • 2011-03-02
    • 2011-01-29
    • 1970-01-01
    • 1970-01-01
    • 2015-04-16
    • 2019-02-21
    • 1970-01-01
    • 2015-06-29
    • 2017-09-12
    相关资源
    最近更新 更多