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.xml.
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

Download Source Code
References
- Jersey Official Website
- RESTEasy hello world example
- REST with Java (JAX-RS) using Jersey – Tutorial
- IBM : RESTful Web services: The basics
- RESTful Web Services







[...] http://www.mkyong.com/webservices/jax-rs/jersey-hello-world-example/ [...]
Hi,
How can I use ant to build Jax-rs. Is it a simple war file or is there some wsgen tasks that I have to run as with Jax-ws. Please could you provide me with a sample as I do not want to start using Maven just now.
Thanks
I’m not sure what we could do without you. your tutorials are just perfect. I have never seen anyone on the web who has written such a good and perfect tutorial.
keep up the good work.
Took a while to get this to work. I noticed along the way that the latest Jersey release does not work like this, the servlet moved. Another great tutorial though, thank you.
Hello and thank you for this tutorial. I haven’t started it yet, because I’m not sure if Maven is required and I don’t want to learn it now. So, will I be able to complete this tutorial without using Maven? All I want is to create a web service (rest + json).
Waiting for your reply. Thank you in advance.
In this tutorial, Maven is nothing but a build tool, you can get all the dependencies manually and build it manually or via Ant.
[...] um tutorial bom e de fácil entendimento você encontra no MKYONG . Mas, porém contudo no entanto pule o passo 2, ao invés disso, crie um Dynamic Web Project no [...]
Thanks Mr. Yong,
For your valuable WS-RS tutorials.I had been hunting such a nice WS-RS example since a long time.So thanks a lot.
your comment is motivated :)
[...] JAX-RS implementation.Happy learning JAX-RS Quick StartSome quick start examples to use JAX-RS.Jersey hello world example Jersey framework to create a simple REST style web application.RESTEasy hello world example [...]
Just so you know, your site is blocking it’s own images ‘Hotlinking detected’
Hi, could you explains it in detail? How to simulate it?