Jersey 2 Hello World Example

Jersey 2 Hello World Example

Jersey is very popular framework to build restful services. This is a basic example to help you getting start with Jersey 2. Technologies used in this examples are :

  1. Maven
  2. Jersey 2.23.1
  3. JDK 1.7
  4. Tomcat 7

1) Add below dependencies in your pom.xml.

<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>code2succeed</groupId>
  <artifactId>code2succeed</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>code2succeed 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>
    <!-- JAX-RS -->
    <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>javax.ws.rs-api</artifactId>
        <version>${jaxrs.version}</version>
    </dependency>
    <!-- Jersey 2.23 -->
    <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>
    <dependency>
	    <groupId>javax.servlet</groupId>
	    <artifactId>javax.servlet-api</artifactId>
	    <version>3.1.0</version>
	</dependency>
  </dependencies>
  <properties>
      <jersey2.version>2.23.1</jersey2.version>
      <jaxrs.version>2.0.1</jaxrs.version>
  </properties>
  <build>
    <finalName>code2ucceed</finalName>
  </build>
</project>

2) Add below servlet mapping configuration into web.xml

<web-app>
  <display-name>Archetype Created Web Application</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.code2succeed.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>

3) Helloworld rest service class.

package com.code2succeed.rest;

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

@Path("/hello")
public class HelloworldService {

	@GET
	public String sayHello() {
		return "Hello World";
	}
}

4) Run the application.

Run the application on any of web application server, I have used tomcat 7 and hit http://localhost:8080/code2succeed/rest/hello using browser.

helloworld jersey 2

Leave a Reply

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