I know I can expose a class inside a web-module as a restful interface using only annotations. Now I want to do something a little more complex.
I’m having a stateless EJB in an ejb-module inside my ear and want to expose this bean as a restful interface using jax-rs. In the first step I annotated the EJB class with @Path and @GET. It didn’t work.
It works when I create an additional web-module with a web.xml that contains
<context-param>
<param-name>resteasy.jndi.resources</param-name>
<param-value>myear/testService/no-interface,myear/testService2/no-interface</param-value>
</context-param>
<listener>
<listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
</listener>
<servlet>
<servlet-name>rest</servlet-name>
<servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>rest</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
I can expose a simple class with only annotations, so I can’t quite believe that I have to explicitly configure every EJB that I want to expose in a config file.
Is there a simpler/better way?
I’m working with JBoss 6.1
There shouldn’t be any config required. If you’re willing to look at another container — at least for reference purposes — here’s an example that exposes an @Singleton EJB as both a JAX-RS service and @LocalBean.
The bean itself uses Container-Managed Transactions and JPA, with the JPA @Entity objects used in the actual JAX-RS messages — effectively turning the EntityManager into a transactional JAX-RS service.
Small chunk of the EJB class:
And here’s a chunk of the unit test for it (uses the Embeddable EJBContainer API):
Full source of the example here. The entire example is just three classes (that includes the test) and a persistence.xml file.