Jersey hello world example

Jersey is reference implementation of JAX-RS (JSR 311) specification.
In this example we will see the bare bone example of using Jersey. Since this is our first example and not a real world application i will be using Grizzly Http Container to deploy our service. Later in our examples we will see how we can deploy Jersey examples on other popular Servlet Containers like Tomcat.

1. Directory Structure

Here is the directory structure of our mavenized Java project.

2. Project Dependencies

<project xmlns="" xmlns:xsi=""




			<name> Repository for Maven</name>
			<name> Snapshot Repository for Maven</name>





3. Rest Service



@Path(value = "/hello")
public class JerseyHelloWorldService {

	public String greet(@PathParam(value = "param") String name){
		return "Hello " + name + ", how are you?";

4. Main Class

Our main class starts the Girzzly Container and deploys our service




import org.glassfish.grizzly.http.server.HttpServer;

import com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory;
import com.sun.jersey.api.core.PackagesResourceConfig;
import com.sun.jersey.api.core.ResourceConfig;

public class MainClass {

	private static URI getBaseURI() {
		return UriBuilder.fromUri("http://localhost/").port(8080).build();

	public static final URI BASE_URI = getBaseURI();

	protected static HttpServer startServer() throws IOException {
		System.out.println("Starting grizzly...");
		ResourceConfig rc = new PackagesResourceConfig(
		return GrizzlyServerFactory.createHttpServer(BASE_URI, rc);

	public static void main(String[] args) throws IOException {
		HttpServer httpServer = startServer();
								"Jersey app started with WADL available at "
										+ "%sapplication.wadl\nTry out %shello/bharat\nHit enter to stop it...",
								BASE_URI, BASE_URI));;

5. Console output

On running our main class grizzly server starts up, spitting some messages on the console

Starting grizzly...
Sep 8, 2012 2:58:05 PM com.sun.jersey.api.core.PackagesResourceConfig init
INFO: Scanning for root resource and provider classes in the packages:
Sep 8, 2012 2:58:05 PM com.sun.jersey.api.core.ScanningResourceConfig logClasses
INFO: Root resource classes found:
Sep 8, 2012 2:58:05 PM com.sun.jersey.api.core.ScanningResourceConfig init
INFO: No provider classes found.
Sep 8, 2012 2:58:05 PM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFO: Initiating Jersey application, version 'Jersey: 1.8 06/24/2011 12:17 PM'
Sep 8, 2012 2:58:06 PM org.glassfish.grizzly.http.server.NetworkListener start
INFO: Started listener bound to [localhost:8080]
Sep 8, 2012 2:58:06 PM org.glassfish.grizzly.http.server.HttpServer start
INFO: [HttpServer] Started.
Jersey app started with WADL available at http://localhost:8080/application.wadl
Try out http://localhost:8080/hello/bharat
Hit enter to stop it...

6. Output on Browser

Here is the output


, , ,

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: