Create RESTful app using JAX-RS

Today we are going to discuss few of the strategies which can be used to make RESTful applications in JAVA.

Introduction :

JSR 311 also called as JAX-RS; is the Java Specification for RESTful web services. The few of the major vendors which implement JAX-RS are :

1.)Jersey : One of the major frameowrks that provide JAX-RS implementations is Jersey.

Jersey contains a REST server and a REST client.

For the incoming requests coming to our servlet container, we would want the ones coming for our RESTful services to go to the servlet which would service these RESTful services.

1.)We put a url filter in web.xml to go the servlet which is implemented by Jersey for RESTful services.
2.)com.sun.jersey.spi.container.servlet.ServletContainer is the class provided by Jersey for this purpost.It extends HTTPSerlvet.
3.) We would also need to tell JAX-RS runtime where to look for the classes which will serve as resources, for that we define the parameter “com.sun.jersey.config.property.package” as init-param, and give value as the package where our resources lie.

<servlet>
        <servlet-name>JerseyRestApplication</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>com.anirudh.rest</param-value>
        </init-param>
 <load-on-startup>1</load-on-startup>
    </servlet>
   <servlet-mapping>
      <servlet-name>JerseyRestApplication</servlet-name>
      <url-pattern>/rest/*</url-pattern>
   </servlet-mapping>

JAX-RS supports the creation of XML and JSON via JAXB.
For use of XML, we just need to define the annotation @XmlRootElement on the POJO class which would be used.JAX-RS supports an automatic mapping from JAXB annotated class to XML and JSON.

For JSON, we dont need to annotate,Jersey does the mappings using the Jackson library.
we just need to put the JSONConfiguration.FEATURE_POJO_MAPPING property to true.

<init-param>
    <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
    <param-value>true</param-value>
</init-param

2.)RESTeasy : RESTeasy is JBOSS provided implementation of JAX-RS specifaction.
1. ) Here we would use org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher to dispatch the servlets and service requests for our application.
2.)By default RESTeasy uses Jettision JAXB adapter for JSON for JSON conversion, which uses Badgerfish convention. However Resteasy also support integration with the Jackson.

3.)REST support in Spring3 MVC
I find this as the easiest approach to implement if you are already using Spring MVC in your application.
If your application uses SpringMVC version 3.0 or more, all you need is to add @ResponseBody
to your method and that will return the object in JSON format.
It uses jackson-json mapper for converting Java objects into JSON and vice versa.
Hence we need to add :

<!-- Jackson JSON Mapper -->
		<dependency>
			<groupId>org.codehaus.jackson</groupId>
			<artifactId>jackson-mapper-asl</artifactId>
			<version>${jackson.version}</version>
		</dependency>

(there is another vendor called as Restlet, but we would not be discussing that)

Very soon I will add more detailed blogs on each of these, and discuss more in details what happens behind the scenes.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s