Friday, 12 August 2011

Developing Restful Web services Using IBM JAX-RS

Java™ API for RESTful Web Services (JAX-RS) is a programming model that provides a mechanism for developing services that follow Representational State Transfer (REST) principles. Using JAX-RS, development of RESTful services is simplified.

The IBM® implementation of JAX-RS provides an implementation of the JAX-RS specification.

IBM JAX-RS includes the following features:
  • JAX-RS server runtime
  • Standalone client API with the option to use Apache HttpClient 4.0 as the underlying client
  • Built-in entity provider support for JSON4J
  • An Atom JAXB model in addition to Apache Abdera support
  • Multipart content support
  • A handler system to integrate user handlers into the processing of requests and responses
Required Jars:
  • jsr311-api.jar
  • commons-lang.jar
  • slf4j-api.jar
  • slf4j-jdk14.jar
  • ibm-wink-jaxrs.jar
Step 1 - Creating the Root Resource
As defined in the JAX-RS specification, by default, resource instances are created per request.  For the JAX-RS runtime environment to create a resource instance, you must have either a constructor with no argument or a constructor with only JAX-RS annotated parameters(@Context,@HeaderParam,@CookieParam,@MatrixParam,@QueryParam,@PathParam). present.
I created a class with no constructor.
public class HelloWorld {

Step 2 – Adding path to resource
Add a annotation to HelloWorld class.  For each annotation, set the value as the part of the URL after the base URL of the application.

public class HelloWorld {

Step 3 – Adding method to resource
 I have created a method called welcomeMessage and annotate the method as GET
public class HelloWorld {

      public String welcomeMessage(){
            return "Welcome to Our Websphere Portal JAX-RS";
Whenever an HTTP GET request is received by this class, the welcomeMessage method will be invoked.

Step 4 – Creating a sub-class
For non-JAX-RS aware web container environments (most environments are currently non JAX-RS aware), a sub-class needs to be created which returns sets of JAX-RS root resources and providers. In the following example, there is only one root resource that will need to be returned.
public class RestConfig extends  Application{
      public Set<Class<?>> getClasses() {
            Set<Class<?>> classes = new HashSet<Class<?>>();
            return classes;
Step 5 – Configure the web.xml file for the JAX-RS application
We need to modify the web.xml so that the servlet container knows that the web application is JAX-RS supported and what are the JAX-RS classes by specifying the application configuration sub class as a parameter.
<web-app id="WebApp_ID" version="2.5" xmlns="" xmlns:xsi="" xsi:schemaLocation="">

click here to download the example

on accessing,

where RestExample is context root

1 comment:

  1. Is there a more "standard" way of defining the servlet? It seems that the reference implementation uses a different servlet.

    Maybe they need to add to the 3.1 spec something were we can define "features" to enable such as JSF or REST.