Home » RESTful Webservices » JAX-RS & Jersey Example using Maven and Eclipse

JAX-RS & Jersey Example using Maven and Eclipse

Jersey is the open source reference implementation of Java JAX-RS specification. It provides a Java library using which we can easily create RESTful web services in Java platform. JAX-RS / Jersey supports JAXB based XML bindings.

Technologies and Tools used

  • Jersey 1.9
  • Tomcat
  • Eclipse
  • JDK 1.8

Follow the steps mentioned below to develop Hello World program using Jersey and JAX-RS web service.

Directory Structure :

JAX-RS example

Step 1 : Create Web Application Project using Maven Template

Create a Java web application project from maven-archetype-webapp template.

mvn archetype:generate -DgroupId=com.jwt.rest -DartifactId=RESTfullApp -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

Step 2 : Eclipse IDE integration

Convert this project to Eclipse web project with Maven command "mvn eclipse:eclipse -Dwtpversion=1.5".Open command prompt and navigate to generated "RESTfullApp" project and issue following command.

mvn eclipse:eclipse -Dwtpversion=1.5

Step 3 : Import the Project into eclipse

Import the project into eclipse IDE.

Step 4 : Update pom.xml file

Add the jersey dependencies in pom.xml.


<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>RESTfullApp Maven Webapp</name>

After updating the pom.xml again execute "mvn eclipse:eclipse -Dwtpversion=1.5". After executing this command Maven will download required libraries.

Note:- After above step refresh your project otherwise eclipse will not recognize the downloaded files.

Step 5 : Create Java directory :

Create a directory named java under main."/src/main". Right click on the project then select New ->Folder and provide the name as "java" as shown below.

JAX-RS example

Now add this directory to class path of your project.

Step 6 : Add java directory to classpath of the Project :

  • Right click on Project -> Build Path -> Configure build path , a new screen will open in that screen click on Source tab and then click on Add folder as shown bellow.

  • JAX-RS example

  • Again one new screen will open and in that screen select the java checkbox and click on OK button as shown bellow.

  • JAX-RS example

    Now Java folder is in classpath of your project.

Step 7 : Create REST Service :

Create a java class "HelloWorldService" in a package com.jwt.rest and add following code into this.


package com.jwt.rest;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
public class HelloWorldService {
	public Response getMsg(@PathParam("name") String name) {
		String output = "Welcome   : " + name;
		return Response.status(200).entity(output).build();

Step 8 . Create Web.xml configuration File

Specify Jersey framework Servlet for our application in web.xml file.In web.xml, register "com.sun.jersey.spi.container.
servlet.ServletContainer", and puts your Jersey service folder under "init-param","com.sun.jersey.config


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">



Run the application :

Right click on project -> run as -> run on server Select Apache tomcat and click on finish as shown below.

JAX-RS example

By default eclipse will open http://localhost:8080
/RESTfullApp/. In web.xml we have specified URL pattern as /rest/* (line number 22) and in HelloWorldService.java we specified class level @path as /hello [ line number 8 ] and method level @path as {name} [ line number 1 ],So the final URL should be "http://localhost:8080

Test your REST service with URl "http://localhost:8080/
RESTfullApp/rest/ hello/java"
.You will get following output:

JAX-RS example

Previous Next Article

comments powered by Disqus