EJB3 Stateless Bean 在 REST-WebService 中始终为 null(Glassfish3、EJB3、Stateless Bean)

发布于 2024-12-27 02:21:33 字数 5384 浏览 0 评论 0原文

我希望你能帮助我解决这个问题: 我有一个使用 Eclipse 创建的 WebProject 作为动态 Web 项目,在 Glassfish3 服务器上运行。我正在使用 EJB 3.0 创建一个无状态外观(@stateless Annotation)来实现我的业务逻辑:

@Stateless
public class Facade {   

public void test(){     
System.out.println("test hat geklappt!!");
}   
}

此外,我正在使用 RESTRessource 来提供我的 REST WS,该 REST WS 使用我的 EJB 和(@EJB Annotation)业务逻辑:

@RequestScoped
@Path("/prescriptions")
public class Ressource {

    @EJB
    private Facade facade;

    public Ressource() {
        super();
    }

    @GET
    @Path("/user/{userid}")
    @Produces(MediaType.APPLICATION_JSON)
    public void getUser(@PathParam("userid") String userid) {

        facade.test();

    }   

}

虽然我可以进入 REST WS 并调用它,我的 EJB 始终为空,我找不到原因。

如果您能帮助我,我将非常感激。如果您需要有关代码或其他信息的更多信息,请询问。

提前致谢, Florian

PS:我的日志中总是出现以下错误:

INFO: Portable JNDI names for EJB Facade : [java:global/TestProject/Facade,java:global/TestProject/Facade!webservices.Facade]   
INFO: Portable JNDI names for EJB Facade : [java:global/TestProject/Facade, java:global/TestProject/Facade!webservices.Facade]
INFO: Scanning for root resource and provider classes in the packages:
webservices
INFO: Root resource classes found:
class webservices.Ressource
INFO: No provider classes found.
INFO: Initiating Jersey application, version 'Jersey: 1.5 01/14/2011 12:36 PM'

SCHWERWIEGEND: The RuntimeException could not be mapped to a response, re-throwing to the HTTP container    
java.lang.NullPointerException

at webservices.Ressource.getUser(Ressource.java:40)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$VoidOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:150)
at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:70)
at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:279)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:136)
at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:86)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:136)
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:74)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1347)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1279)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1229)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1219)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:419)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:699)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:848)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1534)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:326)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:227)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:170)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:822)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:719)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1013)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:662)

I hope you can help me with this:
I have a WebProject created with Eclipse as a dynamic web project, running on a Glassfish3 Server. I’m using EJB 3.0 to create a stateless Façade(@stateless Annotation) that implements my business logic:

@Stateless
public class Facade {   

public void test(){     
System.out.println("test hat geklappt!!");
}   
}

Additionally I’m using a RESTRessource to offer my REST WS that uses my EJB with (@EJB Annotation) the business logic:

@RequestScoped
@Path("/prescriptions")
public class Ressource {

    @EJB
    private Facade facade;

    public Ressource() {
        super();
    }

    @GET
    @Path("/user/{userid}")
    @Produces(MediaType.APPLICATION_JSON)
    public void getUser(@PathParam("userid") String userid) {

        facade.test();

    }   

}

Although I can get into the REST WS and call it, my EJB is always null and I cant find a reason why.

I would really appreciate it if you could help me. In case you need more information about the code or something just ask.

Thanks in advance,
Florian

PS: I always get the following error in my log:

INFO: Portable JNDI names for EJB Facade : [java:global/TestProject/Facade,java:global/TestProject/Facade!webservices.Facade]   
INFO: Portable JNDI names for EJB Facade : [java:global/TestProject/Facade, java:global/TestProject/Facade!webservices.Facade]
INFO: Scanning for root resource and provider classes in the packages:
webservices
INFO: Root resource classes found:
class webservices.Ressource
INFO: No provider classes found.
INFO: Initiating Jersey application, version 'Jersey: 1.5 01/14/2011 12:36 PM'

SCHWERWIEGEND: The RuntimeException could not be mapped to a response, re-throwing to the HTTP container    
java.lang.NullPointerException

at webservices.Ressource.getUser(Ressource.java:40)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$VoidOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:150)
at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:70)
at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:279)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:136)
at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:86)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:136)
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:74)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1347)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1279)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1229)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1219)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:419)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:699)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:848)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1534)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:326)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:227)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:170)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:822)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:719)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1013)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:662)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不忘初心 2025-01-03 02:21:34

理想的 RESTFul 类设计应该是无状态,如 文章

无状态服务不仅性能更好,而且可以改变大部分
维护客户端应用程序状态的责任

如果将 Stateless 注释添加到 Ressource 类,则可以注入 Facade 会话 bean,并且容器将关心会话 bean 的生命周期。

这将使您的代码顺利运行而不会出现异常。

不要忘记让您的 getUser 方法返回一个值!

    public String getUser(@PathParam("userid") String userid) {

        facade.test();

        return "{" + userid + "}";
    }

Ideally RESTFul class design should be stateless, as explained in this article:

A stateless service not only performs better, it shifts most of the
responsibility of maintaining state to the client application

If you add the Stateless annotation to your Ressource class, you can inject your Facade session bean, and the container will care of the lifecycle of your session bean.

This will make your code run smoothly without the exception.

Don't forget to make your getUser method return a value!

    public String getUser(@PathParam("userid") String userid) {

        facade.test();

        return "{" + userid + "}";
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文