Wednesday 26 August 2015

Jersey Hello World Application


In this post, I am going to explain, simple Hello World application.

Step 1: Create new Dynamic web project in eclipse.
File -> New -> Dynamic Web Project.
Give project name as jersey_tutorial, press Next.
Select the check box “Generate web.xml deployment descriptor”. Press Finish.

Step 2: Convert newly created project as maven project.
Right click on the project -> Configure -> Convert To Maven Project.
Use default values and press Finish.
Total Project structure looks like below.
Step 3: Open pom.xml and update dependencies for jersey.

Following are the dependencies I am going to use.
<dependency>
         <groupId>org.glassfish.jersey.containers</groupId>
         <artifactId>jersey-container-servlet-core</artifactId>
         <version>2.21</version>
</dependency>

<dependency>
         <groupId>org.glassfish.jersey.core</groupId>
         <artifactId>jersey-server</artifactId>
         <version>2.21</version>
</dependency>

Following is the pom.xml file.
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/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>jersey_tutorial</groupId>
 <artifactId>jersey_tutorial</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>war</packaging>
 <build>
  <sourceDirectory>src</sourceDirectory>
  <plugins>
   <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <configuration>
     <source>1.8</source>
     <target>1.8</target>
    </configuration>
   </plugin>
   <plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.4</version>
    <configuration>
     <warSourceDirectory>WebContent</warSourceDirectory>
     <failOnMissingWebXml>false</failOnMissingWebXml>
    </configuration>
   </plugin>
  </plugins>
 </build>

 <dependencies>
  <dependency>
   <groupId>org.glassfish.jersey.containers</groupId>
   <artifactId>jersey-container-servlet-core</artifactId>
   <version>2.21</version>
  </dependency>

  <dependency>
   <groupId>org.glassfish.jersey.core</groupId>
   <artifactId>jersey-server</artifactId>
   <version>2.21</version>
  </dependency>
 </dependencies>
</project>


Step 4: Declare the Jersey container Servlet in your Web application's web.xml deployment descriptor file. We are just forwarding all requests to Jersey container.

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://java.sun.com/xml/ns/javaee"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
 id="WebApp_ID" version="3.0">
 <display-name>jersey_tutorial</display-name>
 <welcome-file-list>
  <welcome-file>index.html</welcome-file>
  <welcome-file>index.htm</welcome-file>
  <welcome-file>index.jsp</welcome-file>
  <welcome-file>default.html</welcome-file>
  <welcome-file>default.htm</welcome-file>
  <welcome-file>default.jsp</welcome-file>
 </welcome-file-list>

 <servlet>
  <servlet-name>Jersey REST Service</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.jersey_tutorial</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>Jersey REST Service</servlet-name>
  <url-pattern>/*</url-pattern>
 </servlet-mapping>
</web-app>


Following statements instruct Jersey to scan the package(com.jersey_tutorial) and register any found resources and providers automatically.

<init-param>
         <param-name>jersey.config.server.provider.packages</param-name>
         <param-value>com.jersey_tutorial</param-value>
</init-param>

If you want to add multiple package, add them as comma separated.

<init-param>
         <param-name>jersey.config.server.provider.packages</param-name>
         <param-value>com.jersey_tutorial, com.example</param-value>
</init-param>

Step 5: Create package "com.jersey_tutorial".
        
Step 6: Create HelloWorld class like below.
package com.jersey_tutorial;

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

@Path("hello")
public class HelloWorld {

 private static String message = "Hello World";

 @GET
 public Response sendMessage() {
  return Response.status(200).entity(message).build();
 }
}


@Path("hello")
The @javax.ws.rs.Path annotation placed on the HelloWorld class designates the class as a JAX-RS service. Java classes that you want to be recognised as JAX-RS services must have this annotation. Path annotation identifies the URI path that a resource class or class method will serve requests for. In the above case it matches to URI .http://localhost:8080/jersey_tutorial/hello

@GET
GET annotation indicates that the annotated method responds to HTTP GET requests.

Step 7: It is time to run application on server.
Right click on project -> Run As -> Run On Server.
Run following URL. You will get "Hello World" message on screen.





Prevoius                                                 Next                                                 Home

No comments:

Post a Comment