Course Data project

A JISC funded project aimed at improving course-related data at Sussex University

New features of Java EE 6 used in this project (part 2)

Oct

17

As mentioned in my last post, the XCRI-CAP project has allowed us to use many of the new features of Java EE 6. One of the new features we’ve been able to implement is the RESTful web service support via JAX-RS.  Previously we have implemented JAX-WS web services by turning EJB Session beans into web services. For the XCRI-CAP project, we have implemented a RESTful service using the Jersey implementation of JAX-RS.

JAX-RS makes it very easy to produce a RESTful web service. The first thing to do is to register the Jersey REST servlet with the application’s web.xml.


    <servlet>
        <servlet-name>JerseyRESTService</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>uk.ac.susx.xcricap.session</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>JerseyRESTService</servlet-name>
        <url-pattern>/CourseCatalog/*</url-pattern>
    </servlet-mapping>

This Servlet will be used for any request mapped to /CourseCatalog.

Next, we can create an EJB that will act as the actual REST service. Using the @Path annotation at the top of the Session bean will control which requests will be handled by the class. In this instance, I want all requests to /CourseCatalog to be handled by this Session bean:

/**
 * REST Web Service
 *
 * @author rjb41
 */
@Path(“/”)
@Stateless
public class XCRI_CAPRestService {

Next we can write a method that will receive the RESTful requests. The following method handles Rest parameters sent to the /CourseCatalog/rest/foo/bar URL:


    /**
     * Returns the REST XML representation of the service 
     * @todo specify search criteria
     * @param cId
     * @return 
     */
    @GET
    @Path("/rest/{name}/{value}")
    @Produces(MediaType.TEXT_XML)
    public String getXCRIXml(@PathParam("name") String name, @PathParam("value") String value) {
        if (!(new File(xcriSession.getSearchCourseFileName())).exists()) {
            xcriSession.createCoursesFile(true);
        }
        return outputDocument(xcriSession.searchCatalog(name, value)).toString();
    }

This method treats the first argument as the parameter name and the second as the value to search on, e.g.
XCRI-CAP/CourseCatalog/rest/subject/European would return all courses that contain the word European (the application is actually querying the XML for nodes that end in subject and contain the word ‘European’).

Of course we can do more complex URL searches but for the purposes of demonstration, this shows what is possible.

There are plenty of good tutorials to be found on line, here is the official Oracle documentation:
http://docs.oracle.com/javaee/6/tutorial/doc/gipzz.html

Leave a Reply