First RESTful Webservice

To create RESTful webservice using plain JDK, tomcat and Jersey (JAX-RS sample implementation)

Pre Requisite:
  1. JDK
  2. Tomcat ( any other servlet container)
  3. Jersey - Its sample JAX-RS implementation. It uses servlet controller that handle REST URLS.
Steps :
1. Create eclipse project with following structure
WEB-INF
--classes
--lib
--web.xml

2. Download latest Jersey jars from http://download.java.net/maven/2/com/sun/jersey/jersey-archive/ and extract it under lib folder.

3. Create following java class under src and make sure to set output folder in eclipse to WEB-INF/classes.

package com.rahul.ws;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

@Path("helloworld")
public class HelloWorldResource {
    @GET
    @Produces("text/plain")
    public String getMessage() {
        return "This is sample REST service";
    }
}

4. Add entry into web.xml for Jersey controller


    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
        http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
   
        RestfulContainer
        com.sun.jersey.spi.container.servlet.ServletContainer
       
            com.sun.jersey.config.property.packages
            com.rahul.ws
       

        1
   

   
        RestfulContainer
        /resources/*
   


5. Create war file using "%JAVA_HOME%\bin\jar" cvf restful.war WEB-INF

6. Deploy this war under tomcat and access the service using
    http://localhost:9090/restful/resources/helloworld
7. That's it !!