Home » Enterprise Java » rest » jersey » Creating JAX-RS web service using Jersey Example

About Alvin Reyes

Avatar photo
Alvin has an Information Technology Degree from Mapua Institute of Technology. During his studies, he was already heavily involved in a number of small to large projects where he primarily contributes by doing programming, analysis design. After graduating, he continued to do side projects on Mobile, Desktop and Web Applications.

Creating JAX-RS web service using Jersey Example


Jersey as it states in the website, is more than just a reference implementation. It also has it’s own API that extends the specification toolkit with additional wrapped features and utilities to simplify RESTful service and client development. It also exposes numerous extension SPIs so that developers may extend Jersey to best suit their needs. For this post, we’re going to demonstrate how easy and simple it is to develop a RESTful service using this awesome technology!

What are we doing?

We’re doing a simple account details listing. So We will create a service that will display a json output of account details that can be parsed on the front-end.

1. Create a webapp using Maven and plug in the Jersey library


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<name>jersey-example Maven Webapp</name>


2. Additional configuration

We also need to put additional configurations on the web.xml file as we need the app to recognize our service.


<web-app id="WebApp_ID" version="2.4"
	<display-name>Restful Web Application</display-name>

3. Create the Service

We now create the service. This service is located on the package we specified on the web.xml (com.sun.jersey.config.property.packages).


package com.javacodegeeks.jersey.main;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

public class AccountDetailsService {
    public Response getAccountDetails(@PathParam("param") String accountName) {
    	String output = "Account Name : " + accountName;
		return Response.status(200).entity(output).build();

4. Let’s Test it!

Deploy your app to a J2EE container and go to:

Figure 1: Jersey sample output

Figure 1: Jersey sample output

Download the Eclipse project of this tutorial:

You can download the full source code of this example here : jersey-example

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 ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments