Glassfish v3, EJB 3.1 and Jersey

With Glassfish V3 promoted build 43 (or greater) and the latest Jersey 1.0.3-SNAPSHOT it is now possible to create no-interface-view session beans (or POJO session beans) deployed in the war and those beans may be either (root) resource or provider classes.

A big thanks to Ken Saks who provided the hooks in the Glassfish EJB module so Jersey can dynamically plug-in if Glassfish EJB support is available.

For example one could develop the following root resource class:

@Stateless
@Path("ssb")
public class StatelessSessionRootResource {

    @Context private UriInfo ui; 
    @GET
    public String get() {
        return "GET: " + ui.getRequestUri().toASCIIString();
    }
}

Notice the annotated field, ui,  to obtain an instance of UriInfo. Jersey will defer to JNDI to obtain an instance of the session bean (using the EJB 3.1 portable naming mechanism) and the EJB module will inform Jersey when an instance of the session bean is constructed so that Jersey gets a chance to inject Jersey/JAX-RS artifacts.

Session beans can also be used for sub-resources. For example one can add another resource class:

@Stateless
public class StatelessSessionResource {

    @Context private UriInfo ui;

    @GET
    public String get() {
        return "GET: " + ui.getRequestUri().toASCIIString();
    }
}

and then modify the StatelessSessionRootResource class to be as follows:

@Stateless
@Path("ssb")
public class StatelessSessionRootResource {

    @Context private UriInfo ui;

    @GET
    public String get() {
        return "GET: " + ui.getRequestUri().toASCIIString();
    }

    @EJB StatelessSessionResource r; 
    @Path("sub")
    public StatelessSessionResource getSub() {
        return r;
    }
}

A reference to StatelessSessionResource is injected onto the root resource class and then the sub-resource locator method, getSub, returns that reference.

Alternatively the same functionality can be achieved using the Jersey specific feature of returning the class:

@Path("sub/class")
public Class<StatelessSessionResource> getSubClass() {
    return StatelessSessionResource.class;
}

See here for a simple maven project, which contains the resource classes presented above, and can be built to create a war file to deploy to Glassfish V3. The following URIs will exercise the application:

http://localhost:8080/ejb-1.0/rs/ssb

http://localhost:8080/ejb-1.0/rs/ssb/sub

http://localhost:8080/ejb-1.0/rs/ssb/sub/class

Comments:

Hi,

This was a very valuable post for me, as I need to make some research on glassfish v3, particularly the EJB module.
I would like to ask if it is possible to explain how to convert the maven project into a WAR file please?
I have never been through the process, any help would be greatly appreciated.

Thanks in advance

Gabriel.

Posted by Gabriel Dimech on April 30, 2009 at 06:26 PM CEST #

The maven project builds a war file do:

mvn clean install

and a war file will be placed in the target directory.

You get can maven from here:

http://maven.apache.org/download.html

Paul.

Posted by Paul Sandoz on May 04, 2009 at 03:20 AM CEST #

Hi,

when i throw WebApplicationException from resource class, its always caught as EJBException, thus the client get wrong http error code (500)

@Stateless
@Path("testrs")
public class TestResource
{
@GET
public void throwExc(){
throw new WebApplicationException();

}
}

Posted by Herak on July 23, 2009 at 10:06 AM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

sandoz

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today