JAX-RS Jersey Hello World Example

In this tutorial we will create a simple hello world web service with the JAX-RS reference implementation Jersey,which is the reference implementation of the JSR 311 specification.JAX-RS is part of the Java EE.  REST (REpresentational State Transfer) is a simple stateless architecture that generally runs over HTTP. Now lets see create our first JAX-RS service

 Folder Structure :

  1. Create a Dynamic Web Project RestfulExample and create a package for our src files “com.javainterviewpoint
  2. Place the required jar files under WEB-INF/Lib 


  3. Create the Java classes HelloWorld.java under  com.javainterviewpoint folder.
  4. Place the web.xml  under the WEB-INF directory


import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;

public class HelloWorld 
	public Response dispMessage(@PathParam("param")String msg )
		String output = "Welcome to RESTful Jersey example - "+msg;
		return Response.status(200).entity(output).build();
  • We have annotated @Path(“/hello”) at the class level, which means our HelloWorld service can be reached through the URL …/hello
  • All the GET Request will be handled by the dispMessage() method, which we have annotated with @GET
  • @Path(“/{param}”) will hold the value which will be passed in the URL after ../hello/XXXX, Using the @PathParam we will get the value of the “param” and will be assigned to String msg.
  • Finally we will send our Response back.


 <?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/j2ee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_ID" version="2.4">
 <display-name>Restful Web Application</display-name>

In web.xml, we have to register “com.sun.jersey.spi.container.servlet.ServletContainer“, and mention your  source folder under init-param, “com.sun.jersey.config.property.packages“ so that Jersey will look for the web service classes under the package mentioned.

Lets run our application

Now are good to run our application, add the project to the server. I use Tomcat as my server you are free to choose your own.
        http://localhost:8080/RestfulExample/rest/hello/<<parameter passed>>

The request url has to match http://<<server location>>:<<port>>/<<url-pattern if any>>/<<Path mentioned in java @Path>>/{any value}


Leave a Reply

Your email address will not be published. Required fields are marked *