JAX-RS & Jersey Hello World Example

This tutorial explains how to develop RESTful web services in Java with the JAX-RS reference implementation Jersey.In this tutorial we will discuss how to develop a RESTful hello world web application with Jersey.

Technologies and Tools used

  • Jersey 2.0
  • Tomcat 6.0
  • Eclipse Juno
  • JDK 1.7

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.

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

What is REST ?

REST stands for Representational State Transfer.REST uses HTTP Protocol for data communication. REST relies on stateless, client server and cachebale communication protocol. REST is web standards based architecture for designing networked applications.

REST was introduced and defined by Roy Fielding in 2000. The idea is rather than using complex mechanism such as CORBA,RPC or SOAP to connect applications, simple HTTP protocol is used.

RESTful application use HTTP requests to send data(Create/Update),read data and delete data. Thus REST uses HTTP for all four CRUD operations.

1. Create New Dynamic Web Project

The first step is to create a Dynamic Web Project using Eclipse IDE. Open eclipse IDE and click on File -> New -> Project and select Dynamic Web Project wizard from the wizard list. Now name your project as "RESTfulExample" and Click on Finish button using the wizard window as shown below.

JAX-RS first example

2. Add JAX-RS / Jersey Dependent JAR files

Add following jars in your project build path.

  • asm-3.1.jar
  • jersey-client-1.17.jar
  • jersey-core-1.17.jar
  • jersey-server-1.17.jar
  • jersey-servlet-1.17.jar
  • jsr311-api-1.1.1.jar

3 : Create REST Service :

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


package com.jwt.service;

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();

4. 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.property.packages".


<?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">



Directory Structure of project

JAX-RS example

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/RESTfulExample/ . As 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/RESTfulExample/rest/hello/javawebtutor".

Test your REST service under: "http://localhost:8080/RESTfulExample/rest/hello/javawebtutor".You will get following output in browser:

JAX-RS example

You can download the source code of the example by clicking on the Download link below.

Source + Lib : Download

Related Articles

  1. Maven Overview

comments powered by Disqus