Hello world Rest Service project

Creating Hello world project with JAX-RS and Jersey

Tools and Technologies used


Step 1

Create a new maven Project in Eclipse

In Eclipse,Go to File => New => Other
Select Maven project

Name the project as “JerseyHelloWorld

Select the archetype as maven-archetype-webapp and click on Next

Provide Group Id, Artifact Id and package details as below


Click on Finish.

Step 2

Add the Jersey server dependency to pom.xml

Jersey server is one of the widely used implementation of JAX-RS and hence we will use Jersey implementation in this project.

for details on What is JAX-RS and Jersey, please go through JAX-RS article.

  1. <project xmlns="http://maven.apache.org/POM/4.0.0"
  2.      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
  4.     http://maven.apache.org/maven-v4_0_0.xsd">
  5.   <modelVersion>4.0.0</modelVersion>
  6.   <groupId>Jersy</groupId>
  7.   <artifactId>JersyHelloWorld</artifactId>
  8.   <packaging>war</packaging>
  9.   <version>0.0.1-SNAPSHOT</version>
  10.   <name>JersyHelloWorld Maven Webapp</name>
  11.   <url>http://maven.apache.org</url>
  12.   <dependencies>
  13.     <dependency>
  14.       <groupId>junit</groupId>
  15.       <artifactId>junit</artifactId>
  16.       <version>3.8.1</version>
  17.       <scope>test</scope>
  18.     </dependency>
  20.    <dependency>
  21.     <groupId>org.glassfish.jersey.containers</groupId>
  22.     <artifactId>jersey-container-servlet</artifactId>
  23.     <version>2.24</version>
  24.    </dependency>
  26. <!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
  27.    <dependency>
  28.     <groupId>javax.servlet</groupId>
  29.     <artifactId>servlet-api</artifactId>
  30.     <version>2.5</version>
  31.    </dependency>
  33.  </dependencies>
  35.  <build>
  36.     <finalName>JerseyHelloWorld</finalName>
  37.   </build>
  38. </project>
<project xmlns="http://maven.apache.org/POM/4.0.0" 
  <name>JersyHelloWorld Maven Webapp</name>


<!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->


We have added dependencies for Jersey servlet ,servelt api and Junit in the above pom file.

Step 3

Update web.xml file with Jersey servlet container

we have defined a special servlet called “jersey-serlvet” in web.xml and mapped it by the URL pattern /rest/*

So just like any other servlet in web application,any request matching with the given pattern i.e /rest/* will be redirected to “Jersey servelt”.

  1. <!DOCTYPE web-app PUBLIC
  2.       "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  3.       "http://java.sun.com/dtd/web-app_2_3.dtd" >
  5. <web-app>
  6.     <display-name>Archetype Created Web Application</display-name>
  7.     <servlet>
  8.         <servlet-name>jersey-serlvet</servlet-name>
  9.          <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
  10.         <init-param>
  11.             <param-name>jersey.config.server.provider.packages</param-name>
  12.             <param-value>com.kb.rest</param-value>
  13.         </init-param>
  14.         <load-on-startup>1</load-on-startup>
  15.     </servlet>
  17.     <servlet-mapping>
  18.         <servlet-name>jersey-serlvet</servlet-name>
  19.         <url-pattern>/rest/*</url-pattern>
  20.     </servlet-mapping>
  22. </web-app>
      "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
      "http://java.sun.com/dtd/web-app_2_3.dtd" >

	<display-name>Archetype Created Web Application</display-name>



We have also provided the package of java classes(to be qualified as Rest services) under init-param tag.

This package will be considered by Jersey servlet container to identify the actual service when the request comes in.

Step 4

Create the Hello world Rest service

  1. package com.kb.rest;
  3. import javax.ws.rs.GET;
  4. import javax.ws.rs.Path;
  5. import javax.ws.rs.Produces;
  6. import javax.ws.rs.core.MediaType;
  8. @Path("/hello")
  9. public class HelloWorldRestService {
  11.     @GET
  12.     @Produces(MediaType.TEXT_PLAIN)
  13.     public String getMessage(){
  14.         return "Hello World";
  16.     }
  17. }
package com.kb.rest;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

public class HelloWorldRestService {

	public String getMessage(){
		return "Hello World";

@Path – to specify the root path of the resource which is “/hello

@GET – It’s the HTTP method that is supported by the service

@Produces – It’s used to specify the MIME type that service is returning to client which is Plain Text here.

Note: We can see that the package of this class is “com.kb.rest” which is configured in web.xml

Step 5

Build the project

Right click on Project and select Run as Maven install and make sure that build is success.


Step 6

Deploy the project in the Tomcat server

Right click on server and select Add and Remove


Step 7

Start the server

Step 8

Access the Rest service using below URL



http://localhost:8080/JerseyHelloWorld -> project context path

/rest -> URL pattern that can be handled by Jersey servlet as configured in web.xml

/hello -> The @Path resource mapping string which is pointing to HelloWorldRestService.java as defined in that class.

Download this project RestWithJerseyHelloWorld.zip

About the Author

Karibasappa G C (KB)
Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on