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="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.bharat.examples</groupId>
	<artifactId>java-examples</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>java-examples</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<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>
		<repository>
			<id>snapshot-repository.java.net</id>
			<name>Java.net Snapshot Repository for Maven</name>
			<url>https://maven.java.net/content/repositories/snapshots/</url>
			<layout>default</layout>
		</repository>
	</repositories>

	<build>
		<finalName>java-examples-${version}</finalName>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.6</source>
					<target>1.6</target>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-eclipse-plugin</artifactId>
				<version>2.8</version>
				<configuration>
					<downloadSources>false</downloadSources>
					<downloadJavadocs>false</downloadJavadocs>
					<wtpversion>2.0</wtpversion>
					<sourceExcludes>
						<sourceExclude>**/.svn/**</sourceExclude>
					</sourceExcludes>
				</configuration>
			</plugin>
		</plugins>

	</build>

	<dependencies>

		<dependency>
			<groupId>com.sun.jersey</groupId>
			<artifactId>jersey-server</artifactId>
			<version>1.8</version>
		</dependency>
		<dependency>
			<groupId>com.sun.jersey</groupId>
			<artifactId>jersey-grizzly2</artifactId>
			<version>1.8</version>
		</dependency>
	</dependencies>
</project>

3. Rest Service

package com.bharat.examples.rest;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;

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

	@GET
	@Path(value="/{param}")
	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

package com.bharat.examples.rest;

import java.io.IOException;
import java.net.URI;

import javax.ws.rs.core.UriBuilder;

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(
				"com.bharat.examples.rest");
		return GrizzlyServerFactory.createHttpServer(BASE_URI, rc);
	}

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

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:
  com.bharat.examples.rest
Sep 8, 2012 2:58:05 PM com.sun.jersey.api.core.ScanningResourceConfig logClasses
INFO: Root resource classes found:
  class com.bharat.examples.rest.JerseyHelloWorldService
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

Advertisements

, , ,

  1. Leave a comment

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: