Restful Webservice HelloWorld Jersey 2.x Example

Jersey 1.x team joined with GlassFish and all new upgrades are released under Jersey 2.x. Check the details in Jersey official website. Last tutorial we have seen Restful Webservice example using Jersey 1.x , In this tutorial we will see the same Restful Webservice example with Jersey 2.x . Follow the step by step procedure to migrate from jersey 1.x to jersey 2.x.


Ask your questions in Our Forum

To Ask new Question : Ask Question
Check our existing discussions : Questions & Answers

Create maven web application project and add Jersey dependency in pom.xml. Find Jersey 2.x maven dependency configuration below. If you are new to maven refer our maven tutorials.

Jersey 2.x dependency in pom.xml

First update the pom.xml with jersey 2.x dependency.

<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.ehowtonow</groupId>
  <artifactId>RestTutorials</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>RestTutorials Maven Webapp</name>
  <url>http://maven.apache.org</url>
   <properties>
        <jersey2.version>2.19</jersey2.version>
        <jaxrs.version>2.0.1</jaxrs.version>
    </properties>
  <dependencies>
      <!-- JAX-RS -->
        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>javax.ws.rs-api</artifactId>
            <version>${jaxrs.version}</version>
        </dependency>
        <!-- Jersey 2.19 -->
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>${jersey2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-server</artifactId>
            <version>${jersey2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-client</artifactId>
            <version>${jersey2.version}</version>
        </dependency>
  </dependencies>
  <build>
    <finalName>RestTutorials</finalName>
  </build>
</project>

Configure Jersey 2.x Servlet dispatcher

Second update the web.xml. ServletContainer class moved to “org.glassfish.jersey.servlet” package. Init param to configure Rest class packages name changes to “jersey.config.server.provider.packages”, other annotations are remains same they are as pers JAX-RS standards.

<!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>Restful Webservice Example</display-name>
	<servlet>
		<servlet-name>jersey-serlvet</servlet-name>
		<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
             <param-name>jersey.config.server.provider.packages</param-name>
			<param-value>com.ehowtonow.webservice</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>

RestHelloWorld.java

package com.ehowtonow.webservice;

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

@Path("/hello")
public class RestHelloWorld {
 
 @GET
 @Produces(MediaType.TEXT_PLAIN)
 public String sayHello(){
  return "Welcome to eHowToNow.com";
 }
 
 @GET
 @Path("/{userName}")
 public Response greetUser(@PathParam("userName") String userName)
 {
     String output = "Welcome  : " + userName;
     return Response.status(200).entity(output).build();
 }

}

You can access both rest apis using below url. http://localhost:8080/RestTutorials/rest/hello http://localhost:8080/RestTutorials/rest/hello/Annamalai See the output below.


Ask your questions in Our Forum

To Ask new Question : Ask Question
Check our existing discussions : Questions & Answers

Posts You may Like

  1. Java 7 new features
  2. How to get File Last modified Date in Java
  3. Java 8 Overview and New Features
  4. JSoup to parse body fragment
  5. JSoup DOM methods
  6. How to replace all occurrences of a string using RegEx
  7. Email Receiver using Java Mail API
  8. Java 8 Convert a Stream to List
  9. Sending HTML Email using Java Mail API
  10. JAX-RS @PathParm Web service Example
  11. RESTful Web service to consume JSON and Produce XML – Jersey 1.x
  12. JAX-RS @POST using RESTful web service – Jersey 1.x
  13. How to rotate element of List
  14. Convert JSON to Map using Jackson
  15. Java Code Conventions – Java Best Practice

Be the first to comment

Leave a Reply

Your email address will not be published.


*