Welcome to Java Jersey Tutorial. Recently I started working on a Restful web service project using the JAX-RS Jersey framework.

What is Java Jersey Framework?

Java Jersey project tracks the JAX-RS API, which is used to create Restful web services in Java.

1. Who should use this tutorial?

This tutorial is intended for Java programmers who are interested in developing and deploying Restful Web Services using JAX-RS API and JAXB.

2. Prerequisites

The scope of this tutorial is to use Jersey API for creating Restful web services and invoking the web service using a Java client program and testing web service using the tool.

Basic understanding of Java, Web Services, XML, Maven, and any application server (JBoss/Tomcat) is required to understand the tutorial with ease.

3. Softwares and Tools

  • JDK version 1.8.0_131
  • Apache Maven 3.5.3
  • Mac OS X 10.13.4
  • Tomcat 8.5.16
  • Eclipse Java EE IDE Oxygen 4.7.3

Creating Jersey Eclipse Maven Project

Create a “Dynamic Web Project” in Eclipse and then convert it to the maven project. This will provide us a maven based web application basic project.

I have given GroupId as com.journaldev.jersey and artifactID as my-jersey-project but you can specify anything you like. Once we complete the development of our project, the project structure will look like the below image.


Java Jersey Restful Web Service Project Explanation

1. pom.xml: Project configuration details, note the jersey dependencies provided, other details are common for any similar maven project.

2. EmpRequest.java: Java Bean for the request object. The important thing to note here is the @XmlRootElement annotation to map the class to an XML element.

3. EmpResponse.java: Java bean for the response object.

4. ErrorResponse.java: Java Bean that will be sent as the response in case of an exception.

5. EmpNotFoundException.java: A normal exception class thrown in the web service.

6. web.xml: Deployment descriptor for the web service. So any request with URI https://<HOST>:<PORT>/My-Jersey-Project/rest/* will be processed by Jersey ServletContainer servlet.

The init-param value passed for “com.sun.jersey.config.property.packages” defines the package jersey will look for the web service classes. This property must point to your resources classes. It also looks for the resource classes into the sub-packages.

7. EmpRouter.java: Resource class handling different kinds of request.

  • @Path(“/emp”) – All the requests with URI https://<HOST>:<PORT>/My-Jersey-Project/rest/emp/ will be processed by this resource class.
  • @Path(“/getEmp”) – All the requests with URI https://<HOST>:<PORT>/My-Jersey-Project/rest/emp/getEmp will be processed by this method.
  • @POST – This annotation defines that the HTTP method used should be POST. Some other possible values are @GET, @PUT, @DELETE
  • @Consumes(MediaType.APPLICATION_XML) – The method accepts XML element
  • @Produces(MediaType.APPLICATION_XML) – The method returns XML element

8. EmpNotFoundExceptionMapper.java: Exception Mapper class that maps EmpNotFoundException to Response object. The class should have @Provider annotation. This class should be in the package provided for resource classes in web.xml. Implementation of toResponse() method generates the ErrorResponse object and set it as Entity in Response object with status as INTERNAL_SERVER_ERROR.

Our web service is ready, just build it to create the WAR file and deploy it to the application server.

Jersey Client Example

We can use the Jersey Client to call our web service and get a response programmatically.

EmpClient.java: This is a sample java program through which are invoking our web service. We are using Jersey Client API to invoke the service and based on response status we are parsing response entity to EmpResponse or ErrorResponse class.

Success Response


Error Response



In this post, we learned how to create a REST web service using Jersey API. We also looked into the Jersey Client to invoke our REST APIs through java program.

By admin

Leave a Reply

%d bloggers like this: