Build Restful Webservice in Java using JAX-RS API with jersey 2.x. A Hello World example

This tutorial tells about how to create a hello world restful web service in Java servlet using JAX-RS API with jersey 2.17 with the help of Maven support in Eclipse. Assume that Maven plugin for eclipse is installed and configured.

Brief Steps:

1. Create a Maven Project (JavaRestFul)

2. Update pom.xml file for project dependency

3. Update web.xml file to configure the servlet context path for web service.

4. Write Hello World code for web service

in Detail :

1. Creating a Maven Project (JavaRestFul)  for HelloWorldWebservice.

javajaxrs1 Maven webappjavajaxrs3 maven update

Create java folder under src/main

Project Structure :

JAXRS Project Structure


2. Update pom.xml file with following pom.xml content. Glassfish application server is integrated with JAX-RS API. For other Application server (servlet containers), you need to include the below dependencies.


Also needs to include JAX-RS API and Jersey implementation in the deployed application.

The complete pom.xml file used for this project.

   <project xmlns="" xmlns:xsi=""
  <name>JavaRestFul Maven Webapp</name>








When you save the pom.xml file, all dependencies are downloaded and added to build path. To do manually, right click on pom.xml -> Run As -> maven install

3. Update web.xml file with following web.xml content.

<web-app xmlns:xsi="" xmlns="" xmlns:web=""
xsi:schemaLocation="" id="WebApp_ID" version="3.0">
	<display-name>Java Restful WebService Application</display-name>




You need to tell the package name of the web service to application server.  In the above web.xml, is the  package name of the web service.  rest is the servlet context root

4. Write Hello World code for web service



public class HelloWorldService {

	 public String getMsg() {
	     return "<html><body><h1>Hello World WebService is called</h1></body></html>";

	public Response getMsg(@PathParam("param1") String firstName, @PathParam("param2") String lastName) {

		String output = "Hello Mr. " + firstName + " " + lastName;

		return Response.status(200).entity(output).build();



The @Path annotation ensures that HTTP requests to /service are mapped to the getMsg() method depending upon the number of parameters.

In the above HelloWorldService class, first method does not accept any parameter. Call the first method by


which maps to


JAXRS output1

Second method accepts two parameters. Call the second method, by


which maps to

getMsg(@PathParam("param1") String firstName, @PathParam("param2") String lastName)

eg. http://localhost:8080/JavaRestFul/rest/service/Jegan/Kumar

JAXRS output2


Reference :  Java Restful Webservice

Download War at JavaRESTfulDemo.war

Leave a Reply