如何将EJB 3.1公开为REST Web服务? [英] How to expose an EJB 3.1 as a REST Web Service?

查看:157
本文介绍了如何将EJB 3.1公开为REST Web服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在阅读博客。

I discovered a new feature in java restful when using EJB 3.1 while reading an article at Adam Bien's blog.

事实是,无状态和Singleton bean可以作为根资源公开。但是怎么样
我试图这样做:

The thing is that Stateless and Singleton beans can be exposed as root resources. But how? I tried to do it this way:

@Stateless
@LocalBean
@Path("Hybrid")
public class RESTEJBSample {

    @GET
    @Path("/demo")
    @Produces(MediaType.TEXT_PLAIN)
    public String something() {
        return "I am a Hybrid!!!";
    }

}

当我调用URL http:// localhost:8080 / HybridSample / resources / Hybrid / demo 我得到404错误

When i call the URL http://localhost:8080/HybridSample/resources/Hybrid/demo i get a 404 error.

应用这个,只是为了确保JAXRS在我的项目中工作,我创建了一个简单的pojo资源,只是为了测试它是否正常工作。 >

Appart of this and just to make sure that JAXRS is is working in my project, i created a simple pojo resource just to test if it works fine.

@Path("/genericresource")
public class GenericResource {
    @GET
    @Path("/samplemethod")
    @Produces(MediaType.TEXT_PLAIN)
    public String saySomething() {
        return "Something!";
    }
}

这里当我调用URL http:// localhost:8080 / HybridSample / resources / genericresource / samplemethod 它工作正常!

Here when i call the URL http://localhost:8080/HybridSample/resources/genericresource/samplemethod It works fine!

所以我的问题是:


  • 我的EJB缺少什么,作为一个Web服务资源,如GenericResource类在做什么?

  • what is missing in my EJB so it can work as a web service resource such as the class GenericResource is doing?

是否需要额外的配置?

使用EJB作为Web服务有什么限制?

What are the limitations when using EJB as a web service?

推荐答案

根据关于Jersey RESTFul Web服务的NetBeans教程,您可以决定是否


创建javax.ws.rs.core.Application的子类,所有Rest资源
将由该类自动注册(Java EE 6)

create a subclass of javax.ws.rs.core.Application, all Rest resources will be registered by this class automatically (Java EE 6)


在web.xml中创建默认的Jersey REST servlet适配器。

create default Jersey REST servlet adaptor in web.xml.

我一直使用第二个选择,其中包括将其添加到您的 web.xml

I have always used the second choice, which consists in adding this to your web.xml:

<servlet>
    <servlet-name>ServletAdaptor</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>ServletAdaptor</servlet-name>
    <url-pattern>/resources/*</url-pattern>
</servlet-mapping>

根据我的经验,将REST Web服务暴露为EJB非常有用。您可以随时随地注入,您可以在其中注入您的EntityManager,您甚至可以在某些简单的情况下将其用作DAO。

Exposing your REST Web service as an EJB is, to my experience, extremely useful. You can inject it wherever you like, you can inject in it your EntityManager, and you can even use it as a DAO in some simple situations.

关于您的问题/评论关于功能和限制:在EJB容器中运行的企业bean,无论它们是否部署在war文件中。您可以将JMS ConnectionFactory注入资源,如本节的Java EE 6教程。由于注入了一个ConnectionFactory,您可以发送JMS消息。如果要异步接收JMS消息,则需要定义一个消息驱动Bean,如本节
我从来没有尝试扩展用于Jersey Web服务的相同的EJB,以实现MessageListener接口,但我认为也应该是可能的(如果没有,可以将MDB注入到Jersey根目录无状态bean)。

Concerning your question / comment about features and limitations: enterprise beans run in the EJB container, whether they are deployed in a war file or not. You can inject into them a JMS ConnectionFactory as a resource as explained in this section of the Java EE 6 Tutorial. Thanks to the injection of a ConnectionFactory, you can send JMS messages. If you want to receive JMS messages asynchronously, you need to define a Message-Driven Bean as explained in this section of the above mentioned tutorial. I have never tried to extend the same EJB used for a Jersey web-service in order to implement the MessageListener interface, but I think that should be possible as well (if not, you can inject a MDB into your Jersey root Stateless bean).

最后,您可以使用容器管理的事务,如解释这里。此外,从此NetBeans教程

Finally, you can use Container-Managed transactions as explained here. Moreover, from this NetBeans tutorial:


为了让您可以看到应用程序将使用Java
事务API(JTA)(transaction-type =JTA)。这指定

持久性上下文中的实体的生命周期管理的责任分配给容器。

In order to have you can see that the application will use the Java Transaction API (JTA) (transaction-type="JTA"). This specifies that the responsibility for managing the lifecycle of entities in the persistence context is assigned to the container.



<persistence-unit name="em" transaction-type="JTA">

这篇关于如何将EJB 3.1公开为REST Web服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆