RESTful Web Services with Java

REST stands for REpresentational State Transfer, was first introduced by Roy Fielding in his thesis “Architectural Styles and the Design of Network-based Software Architectures” in year 2000.

REST is an architectural style. HTTP is a protocol which contains the set of REST architectural constraints.

REST fundamentals


  • Everything in REST is considered as a resource.
  • Every resource is identified by an URI.
  • Uses uniform interfaces. Resources are handled using POST, GET, PUT, DELETE operations which are similar to Create, Read, update and Delete(CRUD) operations.
  • Be stateless. Every request is an independent request. Each request from client to server must contain all the information necessary to understand the request.
  • Communications are done via representations. E.g. XML, JSON

RESTful Web Services

RESTful Web Services have embraced by large service providers across the web as an alternative to SOAP based Web Services due to its simplicity. This post will demonstrate how to create a RESTful Web Service and client using Jersey framework which extends JAX-RS API. Examples are done using Eclipse IDE and Java SE 6.

Creating RESTful Web Service

    • In Eclipse, create a new dynamic web project called “RESTfulWS”

Screenshot from 2013-11-07 17-06-05

    • Download Jersey zip bundle from here. Jersey version used in these examples is 1.17.1. Once you unzip it you’ll have a directory called “jersey-archive-1.17.1″. Inside it find the lib directory. Copy following jars from there and paste them inside WEB-INF -> lib folder in your project. Once you’ve done that, add those jars to your project build path as well.
      1. asm-3.1.jar
      2. jersey-client-1.17.1.jar
      3. jersey-core-1.17.1.jar
      4. jersey-server-1.17.1.jar
      5. jersey-servlet-1.17.1.jar
      6. jsr311-api-1.1.1.jar

Screenshot from 2013-11-08 02-54-15

Screenshot from 2013-11-07 17-17-36

    • In your project, inside Java Resources -> src create a new package called “”. Inside it create a new java class called “UserInfo”. Also include the given web.xml file inside WEB-INF folder.



 * @author pavithra

// @Path here defines class level path. Identifies the URI path that 
// a resource class will serve requests for.
public class UserInfo {

 // @GET here defines, this method will method will process HTTP GET
 // requests.
 // @Path here defines method level path. Identifies the URI path that a
 // resource class method will serve requests for.
 // @Produces here defines the media type(s) that the methods
 // of a resource class can produce.
 // @PathParam injects the value of URI parameter that defined in @Path
 // expression, into the method.
 public String userName(@PathParam("i") String i) {

  String name = i;
  return "<User>" + "<Name>" + name + "</Name>" + "</User>";

 public String userAge(@PathParam("j") int j) {

  int age = j;
  return "<User>" + "<Age>" + age + "</Age>" + "</User>";


<?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>  
    • To run the project, right click on it and click on run as ->run on server.
    • Execute the following URL in your browser and you’ll see the output.

Screenshot from 2013-11-08 02-04-07


Screenshot from 2013-11-08 03-09-57

Creating Client

    • Create a package called “”. Inside it create a java class called “UserInfoClient”.



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;

 * @author pavithra
public class UserInfoClient {

 public static final String BASE_URI = "http://localhost:8080/RESTfulWS";
 public static final String PATH_NAME = "/UserInfoService/name/";
 public static final String PATH_AGE = "/UserInfoService/age/";

 public static void main(String[] args) {

  String name = "Pavithra";
  int age = 25;

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

  WebResource nameResource = resource.path("rest").path(PATH_NAME + name);
  System.out.println("Client Response \n"
    + getClientResponse(nameResource));
  System.out.println("Response \n" + getResponse(nameResource) + "\n\n");

  WebResource ageResource = resource.path("rest").path(PATH_AGE + age);
  System.out.println("Client Response \n"
    + getClientResponse(ageResource));
  System.out.println("Response \n" + getResponse(ageResource));

  * Returns client response.
  * e.g : 
  * GET http://localhost:8080/RESTfulWS/rest/UserInfoService/name/Pavithra 
  * returned a response status of 200 OK
  * @param service
  * @return
 private static String getClientResponse(WebResource resource) {
  return resource.accept(MediaType.TEXT_XML).get(ClientResponse.class)

  * Returns the response as XML
  * e.g : <User><Name>Pavithra</Name></User> 
  * @param service
  * @return
 private static String getResponse(WebResource resource) {
  return resource.accept(MediaType.TEXT_XML).get(String.class);
    • Once you run the client program, you’ll get following output.
Client Response 
GET http://localhost:8080/RESTfulWS/rest/UserInfoService/name/Pavithra returned a response status of 200 OK

Client Response 
GET http://localhost:8080/RESTfulWS/rest/UserInfoService/age/25 returned a response status of 200 OK


Reference: RESTful Web Services with Java from our JCG partner Pavithra Siriwardena at the EVIAC blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....

4 Responses to "RESTful Web Services with Java"

  1. Good post, but i think it is better to use Maven to manage the dependencies :)

  2. dev says:

    can you please provide steps for call this web service in adroid client ??

  3. anto says:

    Thank your for this tutorial.
    i have error 404 when i excute ur l ; http://localhost:8080/RESTfulWS/rest/UserInfoService/name/Pavithra
    is it possible to have of your project.
    i aso would like to know whether you use a maven project.

  4. Almet says:

    Very good example. Do you had an example with @POST or authentication?

Leave a Reply

one × 6 =

Java Code Geeks and all content copyright © 2010-2015, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

Get ready to Rock!
To download the books, please verify your email address by following the instructions found on the email we just sent you.