Jersey 2.5 + Maven = easy REST web services

What is covered

In this blog, I DO NOT explain about Jersey.
Here, I explain integrating Jersey with Maven to create a quick RESTful web service.

To know about setting up Maven to create Dynamic Web Project, refer to my log named

Maven + Dynamic Web Project = Ouch. How do I run it on eclipse?

I have used the following softwares
  • Eclipse: Kepler
  • JDK: 1.7
  • Compiled in JDK 1.6
  • Servlet 3.0
  • Tomcat 7

Update pom.xml

The first thing we do is to update pom.xml to load all the JARs required for Jersey.

<dependencies>
  <dependency>
    <groupid>org.glassfish.jersey.containers</groupid>
    <artifactid>jersey-container-servlet</artifactid>
    <version>2.5.1</version>
  </dependency>
</dependencies>

We need to add "jersey-container-servlet-core" as a dependency if we are developing webapp for Servlet version older than Servlet 3.0.

Update we.xml

Add the following servlet mappin
<servlet>
  <servlet-name>Jersey Web Application</servlet-name>
  <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
  <init-param>
   <param-name>jersey.config.server.provider.packages</param-name>
   <param-value>
 org.sample.rest
 </param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>Jersey Web Application</servlet-name>
  <url-pattern>/webresources/*</url-pattern>
 </servlet-mapping>


One of the visible differences between Jersey 1.x and 2.x is the package. Jersey 1.x has package com.sun.jersey, whereas Jersey 2.x has package structure org.glassfish.jersey.

ServletContainer is the servlet that integrates Jersey to web application. And as expected, any URL pattern that matches the goes through the servlet.
The init parameter "jersey.config.server.provider.packages" is a list of packages in which Jersey searches for @Path annotation. These become the RESTful services.

You might want to make sure you are running Servlet 3.0 by looking at the web.xml's schema. If not, you could copy this down

<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>MyWebProject</display-name> </web-app>

Sample Class

Following is a sample class that we could use
package org.sample.rest;

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

@Path("/hello")
public class SampleWebService {
 @GET
 @Produces(MediaType.TEXT_PLAIN)
 public String sayHello() {
  return "Hello, how are you?";
 }
}

Now deploy the application on server, and hit the REST URL. In my case it is
http://localhost:8080/MyWebProject/webresources/hello

It works!

You will see "Hello, how are you?" written on your browser :)

I hope this works for you. But if it doesn't, make sure you follow the steps mentioned in another blog entry "Maven + Dynamic Web Project = Ouch. How do I run it on eclipse?"

Feel free to contact me at harsh.curse@gmail.com.

Thanks!

Comments

  1. if the version number is 2.22.1, then jersey-container-servlet-core is automaticaly included in jersey-container-servlet.

    org.glassfish.jersey.containers
    jersey-container-servlet
    2.22.1

    ReplyDelete

Post a Comment