How to open EJB 3.1 as a REST web service?

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

The fact is that Stateeless and Singleton beans can be displayed as root resources. But how? I tried to do it as follows:

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

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

Appart this and just to make sure JAXRS works in my project, I created a simple pojo resource to check if it works fine.

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

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

So my questions are:

  • what is missing in my EJB so that it can work as a web service resource, for example, does the GenericResource class do?

  • Is additional configuration required?

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

+8
java web-services jax-rs ejb
source share
1 answer

According to this NetBeans Jersey web services tutorial RESTFul , you can decide whether to

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

or

create the REST Jersey network servlet adapter in web.xml.

I always used the second option, which is to add it 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> 

Providing your REST web service as an EJB is, in my experience, extremely helpful. You can enter it wherever you want, you can enter your EntityManager into it, and you can even use it as a DAO in some simple situations.

Regarding your question / comment about features and limitations: enterprise beans runs in an EJB container, regardless of whether they are deployed in a war file or not. You can enter JMS ConnectionFactory as a resource in them, as described in this section of the Java EE 6 tutorial. By entering ConnectionFactory, you can send JMS messages. If you want to receive JMS messages asynchronously, you need to define a Message-Driven Bean as described in this section of the above tutorial. I have never tried to extend the same EJB used for a web service in Jersey to implement the MessageListener interface, but I think it should also be possible (if not, you can insert the MDB into your root center without a bean proxy) .

Finally, you can use container-driven transactions as described here . Also from this NetBeans tutorial :

So you can see that the application will use the Java Transaction API (JTA) (transaction type = "JTA"). This indicates that responsibility for managing the life cycle of objects in the context is assigned to the container.

 <persistence-unit name="em" transaction-type="JTA"> 
+11
source share

All Articles