RESTful webservice using Jersey

Here I am going to give an example on how REST webservice works.


The most important concept in REST is resources, which are identified by global IDs — typically using URIs. Client applications use HTTP methods (GET/ POST/ PUT/ DELETE) to manipulate the resource or collection of resources. A RESTful Web service is implemented using HTTP and the principles of REST. Typically, a RESTful Web service should define the following aspects:

The base/root URI for the Web service such as http://<host>/<appcontext/contextpath>/<url pattern>/<resources>.
The MIME type of the response data supported, which are JSON/XML/TEXT/HTML etc.
The set of operations supported by the service. (for example, POST, GET, PUT or DELETE).


HTTP methods are mapped to CRUD (create, read, update and delete) actions for a resource. Although you can make slight modifications such as making the PUT method to be create or update, the basic patterns are listed as follows.

HTTP GET: Get/List/Retrieve an individual resource or a collection of resources.
HTTP POST: Create a new resource or resources.
HTTP PUT: Update an existing resource or collection of resources.
HTTP DELETE: Delete a resource or collection of resources.

Eclipse, JDK 1.6, JAX-RS jars 1.8

1. create dynamic web project in Eclipse
2. Download JAX-RS for REST jar bundle and put the below jars into lib directory under WEB-INF/lib directory

REST webservice example using Jersey

3. Now create domain or model object for Book. Here I have put @XmlRootElement for easy marshalling and unmarshalling using JAXB.


4. create singleton using enum for data provider because I am not using database here for CRUD operations.


5. create BooksResource class for business logic.

Resource Class: class is a plain old java object (POJO) and is not restricted from implementing any interface. This adds many advantages such as reusability and simplicity.
Annotations: They are defined in*, which are part of the JAX-RS (JSR 311) specification.
@Path: This defines the resource base URI. Formed with context root and hostname, the resource identifier will be something like http://localhost:8080/rest/service/books.
@GET: This means that the following method responds to the HTTP GET method.
@POST: This means that the following method responds to the HTTP POST method.
@Produces: Defines the response content MIME type such as plain/text,application/json,application/xml etc.


6. You need to direct all the REST requests to the Jersey container by defining a servlet dispatcher in the application’s web.xml file. Along with the Jersey servlet, we need to also define an initialization parameter indicating the Java package that contains the resources.


Now we need to test the REST service so create the following classes

7. Create Constant class which holds all the constants used in this application


8. Create java class with main method which invokes the REST services.


That’s all. Thanks for your patience. Please feel free to ask any query. Please do not forget to leave a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.