HowToDoInJava

  • Java 8
  • Regex
  • Concurrency
  • Best Practices
  • Spring Boot
  • JUnit5
  • Interview Questions

Jersey Hello World Example

By Lokesh Gupta | Filed Under: Jersey

I have written a number of posts on JAX-RS RESTEasy concepts and how to. Now I have started exploring Jersey which is another popular framework for making RESTFul applications. To start with, I am writing my hello world application in this post, which I will modify in next posts to show demos of other features Jersey provide.

Step 1) Make a eclipse web project using maven

Learn to make eclipse maven project here

Step 2) Update Jersey dependencies in pom.xml file

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.howtodoinjava</groupId>
  <artifactId>JerseyHelloWorld</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>JerseyHelloWorld Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <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>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
		<groupId>com.sun.jersey</groupId>
		<artifactId>jersey-server</artifactId>
		<version>1.17.1</version>
	</dependency>
	<dependency>
		<groupId>com.sun.jersey</groupId>
		<artifactId>jersey-core</artifactId>
		<version>1.17.1</version>
	</dependency>
	<dependency>
		<groupId>com.sun.jersey</groupId>
		<artifactId>jersey-servlet</artifactId>
		<version>1.17.1</version>
	</dependency>
  </dependencies>
  
  <build>
    <finalName>JerseyHelloWorld</finalName>
	    <plugins>
		    <plugin>
		      <artifactId>maven-compiler-plugin</artifactId>
		        <configuration>
		          <source>1.6</source>
		          <target>1.6</target>
		        </configuration>
		    </plugin>
	  </plugins>
  </build>
</project>

Step 3) Update web.xml file with servlet mapping to com.sun.jersey.spi.container.servlet.ServletContainer

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>

  <display-name>Archetype Created 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.howtodoinjava.rest</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
 
	<servlet-mapping>
		<servlet-name>jersey-serlvet</servlet-name>
		<url-pattern>/rest-points/*</url-pattern>
	</servlet-mapping>
	
</web-app>

Step 4) Write first REST service class

The service class will look like exactly as RESTEasy because of common annotations used from JAX-RS.

package com.howtodoinjava.rest;

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

@Path("/show-on-screen")
public class JerseyHelloWorldService
{
	@GET
	@Path("/{message}")
	public Response getMsg(@PathParam("message") String msg)
	{
		String output = "Message requested : " + msg;
		//Simply return the parameter passed as message
		return Response.status(200).entity(output).build();
	}
}

Test the application

To test the application type below URL in browser: http://localhost:8080/JerseyHelloWorld/rest-points/show-on-screen/HowToDoInJava_Dot_Com

This will call the REST API method in service class and print the message passed as path parameter.

Jersey hello world message
Jersey hello world message

To download source of above post, click below link.

Sourcecode Download

Happy Learning !!

About Lokesh Gupta

Founded HowToDoInJava.com in late 2012. I love computers, programming and solving problems everyday. A family guy with fun loving nature. You can find me on Facebook, Twitter and Google Plus.

Feedback, Discussion and Comments

  1. sushil kumar verma

    November 23, 2018

    There is no explanation about classes and how the Web services works. I faced questions like How web services works internally , I tis dfficult to find answer of this questionson internet 🙁 , can you please help on this

    Reply
  2. gani

    January 7, 2014

    prevent displaying null attributes in json response returned from server using jersey jax rs

    Reply
  3. Surya Krupa

    November 21, 2013

    Really helpful thank you!

    Reply

Ask Questions & Share Feedback Cancel reply

Your email address will not be published. Required fields are marked *

*Want to Post Code Snippets or XML content? Please use [java] ... [/java] tags otherwise code may not appear partially or even fully. e.g.
[java] 
public static void main (String[] args) {
...
}
[/java]

Search Tutorials

  • Email
  • Facebook
  • RSS
  • Twitter

Jersey Tutorial

  • Jersey – Hello World
  • Jersey2 – Hello World
  • Jersey – quickstart-archetype
  • Jersey – Custom Logging
  • Jersey – Set Cookie
  • Jersey – File Download
  • Jersey – File Upload
  • Jersey – Multi-File Upload
  • Jersey – Exception Handling
  • Jersey – MOXy JSON
  • Jersey – JSONP
  • Jersey – Google Gson
  • Jersey – Security

Jersey Client

  • Jersey Client – Access REST APIs
  • Jersey Client – Authentication
  • Jersey Client – Set Cookie

Popular Tutorials

  • Java 8 Tutorial
  • Core Java Tutorial
  • Java Collections
  • Java Concurrency
  • Spring Boot Tutorial
  • Spring AOP Tutorial
  • Spring MVC Tutorial
  • Spring Security Tutorial
  • Hibernate Tutorial
  • Jersey Tutorial
  • Maven Tutorial
  • Log4j Tutorial
  • Regex Tutorial

Meta Links

  • Advertise
  • Contact Us
  • Privacy policy
  • About Me

Copyright © 2016 · HowToDoInjava.com · All Rights Reserved. | Sitemap