Create RESTful web services in java(JAX-RS) using jersey

In this post, on  requestwe will develop RESTful web service using jersey in eclipse

Web service Tutorial Content:

    Introduction to web services Web services interview questions SOAP web service introduction RESTful web service introduction Difference between SOAP and REST web services SOAP web service example in java using eclipse JAX-WS web service eclipse tutorial JAX-WS web service deployment on tomcat Create RESTful web service in java(JAX-RS) using jersey RESTful web service JAXRS json example using jersey RESTful web service JAXRS CRUD example using jersey AngularJS RESTful web service JAXRS CRUD example using $http RESTful Web Services (JAX-RS) @QueryParam Example Spring Rest simple example Spring Rest json example Spring Rest xml example Spring Rest CRUD example

Java API for RESTful Web Services (JAX-RS), is a set if APIs to developer REST service. JAX-RS is part of the Java EE6, and make developers to develop REST web application easily.

Jersey is the reference implementation for this specification. Jersey contains basically a REST server and a REST client. The core client can communicate with the server using jersey lib.

On the server side Jersey uses a servlet which scans predefined classes to identify RESTful resources. Via the web.xml configuration file for your web application.

The base URL of this servlet is:
This servlet analyzes the incoming HTTP request and selects the correct class and method on request. This selection is based on annotations provided in the class and methods. 


1) Open eclipse.
2) Create new dynamic web project named "RESTfulWebServiceExample"

3) Now go to location where you have download jersey and go to jersey-archive-1.17->lib can have all jars but for now you can copy following jars
  • asm-3.1
  • jersey-client-1.17
  • jersey-core-1.17
  • jersey-server-1.17
  • jersey-servlet-1.17
  • jsr311-api-1.1.1
Paste all above copied jars to WebContent->WEB-INF->lib

Add all these jars to eclipse build path.
Right click on project(RESTfulWebServiceExample)->properties

Click on Java Build Path and then Add jars as shown in above diagram.

go to project->WebContent->WEB-INF->lib and select all jars then click on ok.

Click ok.Jersey jars added to class path.
4) Create new package named "org.arpit.javapostsforlearning.webservice"


package org.arpit.javapostsforlearning.webservices;
* @author Arpit Mandliya
public class FeetToInchAndInchToFeetConversionService {
      public String convertInchToFeet(@PathParam("i") int i) {
        int inch=i;
        double feet = 0;
        feet =(double) inch/12;
        return "<InchToFeetService>"
        + "<Inch>" + inch + "</Inch>"
          + "<Feet>" + feet + "</Feet>"
         + "</InchToFeetService>";
      public String convertFeetToInch(@PathParam("f") int f) {
       int inch=0;
          int feet = f;
          inch = 12*feet;
          return "<FeetToInchService>"
            + "<Feet>" + feet + "</Feet>"
            + "<Inch>" + inch + "</Inch>"
            + "</FeetToInchService>";

@Path(/your_path_at_class_level) : Sets the path to base URL + /your_path_at_class_level. The base URL is based on your application name, the servlet and the URL pattern from the web.xml" configuration file.

@Path(/your_path_at_method_level): Sets path to base URL + /your_path_at_class_level+ /your_path_at_method_level

@Produces(MediaType.TEXT_XML [, more-types ]): @Produces defines which MIME type is delivered by a method annotated with @GET. In the example text ("text/XML") is produced.

@PathParam: Used to inject values from the URL into a method parameter.This way you inject inch in convertFeetToInch method and convert that to feet.
6)Now you need to create web.xml and put it under /RESTfulWebserviceExample/WebContent/WEB-INF/
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" id="WebApp_ID" version="2.5">
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-name>Jersey REST Service</servlet-name>
In above <param-value>,put your web service package.
8) Run project:right click on project->run as ->run on server
9) Test your REST service under: "http://localhost:8080/RESTfulWebServiceExample/rest/ConversionService/FeetToInch/2". 

You will get output as :

If You see web service information page then you are done.

Creating a Restful Web Service Client:

Create under org.arpit.javapostsforlearning.websevices.client

package org.arpit.javapostsforlearning.webservices.client;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;

public class ConversionServiceClient {
 static final String REST_URI = "http://localhost:8080/RESTfulWebServiceExample";
 static final String INCH_TO_FEET = "/ConversionService/InchToFeet/";
 static final String FEET_TO_INCH = "/ConversionService/FeetToInch/";

 public static void main(String[] args) {

 int inch=12;
 int feet=2;

 ClientConfig config = new DefaultClientConfig();
 Client client = Client.create(config);
 WebResource service = client.resource(REST_URI);

 WebResource addService = service.path("rest").path(INCH_TO_FEET+inch);
 System.out.println("INCH_TO_FEET Response: " + getResponse(addService));
 System.out.println("INCH_TO_FEET Output as XML: " + getOutputAsXML(addService));

 WebResource subService = service.path("rest").path(FEET_TO_INCH+feet);
 System.out.println("FEET_TO_INCH Response: " + getResponse(subService));
 System.out.println("FEET_TO_INCH Output as XML: " + getOutputAsXML(subService));


 private static String getResponse(WebResource service) {
 return service.accept(MediaType.TEXT_XML).get(ClientResponse.class).toString();

 private static String getOutputAsXML(WebResource service) {
 return service.accept(MediaType.TEXT_XML).get(String.class);

Run above program
INCH_TO_FEET Response: GET http://localhost:8080/RESTfulWebServiceExample/rest/ConversionService/InchToFeet/12 returned a response status of 200 OK
INCH_TO_FEET Output as XML: <InchToFeetService><Inch>12</Inch><Feet>1.0</Feet></InchToFeetService>
FEET_TO_INCH Response: GET http://localhost:8080/RESTfulWebServiceExample/rest/ConversionService/FeetToInch/2 returned a response status of 200 OK
FEET_TO_INCH Output as XML: <FeetToInchService><Feet>2</Feet><Inch>24</Inch></FeetToInchService>

Source code :

click to begin

Written by Arpit:

If you have read the post and liked it. Please connect with me on Facebook | Twitter | Google Plus


Java tutorial for beginners Copyright © 2012