Thursday, November 8, 2012

Developing a RESTful Java Web service in Eclipse Indigo using maven

RESTful web services have become the kind of de-facto in the present days, thanks to Roy Fielding's dissertation stating its advantages. Developing these is also easy to someone who is NOT very accustomed to SOAP-WSDL based services. However it is little difficult to someone who has been working in the arena of SOAP, although not tough.
I believe a great deal of difficulty in developing RESTful web services is because of the unavailability of a uniform IDE that makes life easier for a developer. Although Netbeans has a very good abstracted mechanism that lets users develop RESTful web services with no pain, I don't prefer working with it, nor would I recommend anyone to use it. The major reason for the same is the highest levels of abstraction it offers to the developer which makes it difficult for him/her to understand the flow.
In this article I will try to walk you through the steps involved in successfully developing a RESTful java web service in eclipse using maven.
This article uses Eclipse Indigo, maven2, Sonatype M2Eclipse, Apache Tomcat 7.
Step1: Go to Help->Install New Software->Add. Enter the name as "m2eclipse" (you can give any name you wish to) and the location  http://m2eclipse.sonatype.org/sites/m2e. Then you will find the software "Maven Integration for Eclipse". Install it.
Step2: Developing a Dynamic web project also requires installing the software, "Maven Integration for Eclipse WTP", which can be found in the repository, http://m2eclipse.sonatype.org/sites/m2e-extras. Proceed similarly to add this repository too.
Step3: Now choose the directory where the workspace needs to be stored and run the folowing command.
mvn -Declipse.workspace= eclipse:add-maven-repo
This will be the local maven repository.
Step4: Now New->Project->Maven Project->Select Artifact ID as maven-archetype-webapp->Give groupID (similar to package name), artifact ID (similar to project name) and click Finish.
Step5: Now a directory structure will be formed as follows.

Step6: This is the most important step. The directory structure formed by default has to be changed a little. As per the Maven directory structure specifications, all the source java files (services) need to be inside /src/main/java. Follow the following steps to change the directory structure.

  • Remove src/main/resources under Java Resources
  • Add java folder to src/main directory
  • Right click on Java Resources->New->Source Folder and give the previously added java folder in the Folder name and click Finish as shown below.


  • The final directory structure should look like this.


Step7: Now add the following lines to pom.xml.
  

 
  maven2-repository.java.net
  Java.net Repository for Maven
  http://download.java.net/maven/2/
  default
 

  
    
   com.sun.jersey
   jersey-server
   1.8
    
  

This will essentially indicate the maven, the url to look for repositories while building. Also the dependency here is as required by the Jersey specification.
Step8: Now write the java service in a class file. Create a new class and add code similar to this. The annotations @Path along with @GET and @PathParam are very important, you can look into JSR311 API for details on this.
package com.rest.test;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
 
@Path("/test")
public class TestRest {
 
 @GET
 @Path("/{name}")
 public Response getMsg(@PathParam("name") String name) {
 
  String output = "Hello, " + name;
 
  return Response.status(200).entity(output).build();
 
 }
 
}

 Step9: Now we are required to update web.xml according to Jersey reference implementation. web.xml should look like this.

 Restful Web Application
 
 
  TestServlet
  
                     com.sun.jersey.spi.container.servlet.ServletContainer
                
  
       com.sun.jersey.config.property.packages
       com.rest.test
  
  1
 
 
 
  TestServlet
  /resources/*
 
 


These steps should essentially deploy a RESTful java web service successfully and you can test it by hitting the following URL.
http://localhost:8080/resources/test/kausal
And you should see the output "Hello, kausal". Here "kausal" is the parameter passed to the method response().

I hope this gives basic idea about how to develop a RESTful java web service with Jersey reference implementation in eclipse using maven. Please feel free to post your queries/comments.