Home » RESTful Webservices » JAX-RS & Jersey Hello World Example

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
  • Eclipse
  • JDK 1.8

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.

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.

HelloWorldService.java


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;
 
@Path("/hello")
public class HelloWorldService {
 
	@GET
	@Path("/{name}")
	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".

web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>RESTfulExample</display-name>
 <servlet>
		<servlet-name>jersey-serlvet</servlet-name>
		<servlet-class>
                     com.sun.jersey.spi.container.servlet.ServletContainer
                </servlet-class>
		<init-param>
		     <param-name>com.sun.jersey.config.property.packages</param-name>
		     <param-value>com.jwt.service</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>jersey-serlvet</servlet-name>
		<url-pattern>/rest/*</url-pattern>
	</servlet-mapping>

</web-app>

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


Previous Next Article

comments powered by Disqus