Jersey hello world example
Jersey, reference implementation to develope RESTful web service based on the JAX-RS (JSR 311) specification.
In this tutorial, we show you how to develop a simple hello world REST web application with Jersey.
Technologies and Tools used in this article:
- Jersey 1.8
- JDK 1.6
- Tomcat 6.0
- Maven 3.0.3
- Eclipse 3.6
If you want to know what and how REST works, just search on Google, ton of available resources.
1. Directory Structure
This is the final web project structure of this tutorial.

2. Standard Web Project
Create a standard Maven web project structure.
mvn archetype:generate -DgroupId=com.mkyong.rest -DartifactId=RESTfulExample
-DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
To support Eclipse, use Maven command :
mvn eclipse:eclipse -Dwtpversion=2.0
3. Project Dependencies
Jersey is published in Java.net Maven repository. To develop Jersey REST application , just declares “jersey-server” in Maven pom.xm
l.
File : pom.xml
<project ...>
<repositories>
<repository>
<id>maven2-repository.java.net</id>
<name>Java.net Repository for Maven</name>
<url>http://download.java.net/maven/2/</url>
<layout>default</layout>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.8</version>
</dependency>
</dependencies>
</project>
4. REST Service
Simple REST service with Jersey.
package com.mkyong.rest;
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("/{param}")
public Response getMsg(@PathParam("param") String msg) {
String output = "Jersey say : " + msg;
return Response.status(200).entity(output).build();
}
}
5. web.xml
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
“.
File : web.xml
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Restful Web Application</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.mkyong.rest</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>
6. Demo
In this example, web request from “projectURL/rest/hello/” will match to “HelloWorldService“, via @Path("/hello")
.
And the “{any values}” from “projectURL/rest/hello/{any values}” will match to parameter annotated with @PathParam
.
URL : http://localhost:8080/RESTfulExample/rest/hello/mkyong

Leave a Reply
When I am trying to run the same example. I am getting 404 error on server.
I am getting this error when I try to run, HTTP Status 500 – Servlet.init() for servlet jersey-serlvet threw exception
The ResourceConfig instance does not contain any root resource classes.
Anyone can help me here to sort out the issue??.
Changing http://localhost:8080/RESTfulExample/rest/hello/mkyong to
http://localhost:8080/rest/hello/mkyong
made it work for me!
Thanks so much mkyong
IF you are using TOmcat 8.5 and Jersey 1.19 then you need to add the below 2 dependecies:
com.sun.jersey
jersey-server
1.19
com.sun.jersey
jersey-servlet
1.19
If you download his project folder and try to run his, make sure that you spec’s match his, especially the jdk, Jersey and Tomcat version.
CORRECTION:
http://localhost:8080/RESTfulExample/rest/hello/mkyong
(returns a 404 for me)
SHOULD BE
http://localhost:8080/rest/hello/mkyong
(works fine)
This is what got mine to work.
Thanks so much mkyong!
Hi mkyong,
I am getting the following exception. I am completely blocked can you please help me here.
com.sun.jersey.api.container.ContainerException: No WebApplication provider is present
com.sun.jersey.spi.container.WebApplicationFactory.createWebApplication(WebApplicationFactory.java:69)
com.sun.jersey.spi.container.servlet.ServletContainer.create(ServletContainer.java:412)
com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.create(ServletContainer.java:327)
For those of you having trouble:
1. Make sure you are not mixing jersey 1 with jersey 2.
2. The maven dependency for jersey 1 used in this tutorial INCLUDES
javax.ws.rs, so if you have this dependency below in your pom.xml,
you are in for trouble due to library collisions.
javax.ws.rs
javax.ws.rs-api
The error you get won’t be indicative that this is the problem.
More on stack overflow:
http://stackoverflow.com/questions/23277429/exception-in-rest-jersey
as usual an incomplete broken sample!
Not true
To run the demo, do as following steps: 1. Get a servlet container. I use eclipse jetty to do this, and of course tomcat is fine too. 2. cd into base dir of this example and run “mvn clean install”. If you are luck enough, you will get a file named as “RESTfulExample.war” in the target folder. 3. move this file into jetty_home/webapps, and start jetty using command ” java -jar start.jar”. 4 Now, you can access the url refered in author’s blog.
hi Mkyong, I am trying above sample in my weblogic server….i am facing some issue. Is there i need to add any new jars related to weblogic server.
Not work
Hi mkyong I had my XML which was generated using SOAP UI, where I am having some global parameters set. I am trying to execute that XML from maven using continuous integration process I am getting the following error GET – Request_GetAllUsers FailedSubmitException: com.eviware.soapui.model.iface.Request$SubmitException: com.eviware.soapui.impl.wsdl.submit.RequestTransportRegistry$MissingTransportException: Missing protocol in endpoint [${Endpoint}]
Can you help me to over come this
works for me, thanks
And I forgot to configure my server :( Done it now and the example is working perfectly :) … Big-ups to Mkyong
Hi, MK, Thanks for delivering such great work. I have been benefiting from your tutorials for a while now. I am currently trying to get this one to work and be able to understand thoroughly. If I may ask, where do we configure the port number? ‘8080’ in your example?
By default your server will be configured with 8080 port.
its a cool one..
Why are you all using this com.sun packages?? they are likely to be removed from api!!
This was all I needed to get started in the right direction. Thanks!
This example to uses qith JBoss 6 I need to change the web.xml
jersey-serlvet
com.sun.jersey.spi.container.servlet.ServletContainer
com.sun.jersey.config.property.packages
mx.com.sia.tutorial.rest
1
jersey-serlvet
/*
resteasy.scan
false
resteasy.scan.providers
false
resteasy.scan.resources
false
Saludos!!!!
Hi Sir, This is nagarjuna working for MNC, i have copied this project into my eclipse and converted into maven .
it is not working, am referring to different url. can you provide settings .xml for this project
very helpfull, thank you.
Hi, i am getting the below deployment exception when i deployed in websphere 8.0, but it works well in tomcat server. Could you please let me know what was the issue?
java.lang.NoSuchMethodError: javax/ws/rs/core/Application.getProperties()Ljava/util/Map;
at org.glassfish.jersey.server.ApplicationHandler.(
java.lang.NoSuchMethodError: javax/ws/rs/core/Application.getProperties()Ljava/util/Map;
at org.glassfish.jersey.server.ApplicationHandler.(
ApplicationHandler.java:287)
Hi everyone,
i just created this example in my machine, and when i tried to access it via the example URL mentioned below, i am getting Bad request 400 error in all the browsers.
http://localhost:8080/RESTfulExample/rest/hello/Raj
Did anyone faced this issue? If so, any help would be great.
Find below the request from browser:
Remote Address: [::1]:8080
Request URL: http://localhost:8080/RESTfulExample/rest/hello/Rajkumar
Request Method: GET
Status Code: 400 Bad Request
Thanks in advance…
Is it jersey-servlet or jersey-servlvet
Thanks!
Thank you! :) This works.
this service runs perfectly on local server(localhost:8080).. but hen deployed on actual webserver(xyz.com) shows error
The requested URL “rest/hello/mkyong” was not found on this server.
Please help me out
Followed up the deployment process but still getting same error
Thanks Mkyong! unblocked me bigtime…
Dear Friends,Spring Boot vs Spring Web Services vs Jersey web services Which one is better to develop Web Services to develop application
Check on
1)Performance wise
2)Configuration wise
3)Writing Coding wise
Troubleshooting class not found:
you should have appropriate servlet container, param
For Jersey 1.x, example:
com.sun.jersey.spi.container.servlet.ServletContainer
com.sun.jersey.config.property.packages
sample.hello.resources
1
For Jersey 2.x, example:
org.glassfish.jersey.servlet.ServletContainer
and jersey.config.server.provider.packages
Troubleshooting 404 error:
-Make sure your jars are in webapp/lib
why if you are using maven???