Thursday Apr 02, 2009

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

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