Wednesday, May 14, 2014

Developing simple Webservices using Jersey


Jersey is an open source RESTful webservice framework in Java. It provides lots of utilities and features to simplify the RESTful service development. Here, I will explain how to develop a simple hello world program using Jersey 2.x. 

1. Download the Jersey JAX-RS 2.0 RI bundle from https://jersey.java.net/download.html

2. Create a dynamic web project in Eclipse. Keep all the JAR files downloaded in step 1 in the WebContent/WEB-INF/lib folder.

3. Create a web.xml file inside WebContent/WEB-INF directory as follows.

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  
<servlet>
    <servlet-name>myAction</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.test.jersey.action</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet> 
<servlet-mapping>
    <servlet-name>myAction</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>


In the above web.xml, any requests coming to this application will be routed to the servlet class org.glassfish.jersey.servlet.ServletContainer which will redirect the request to appropriate Java class present inside the package com.test.jersey.action. 

If you are using Jersey 1.x version, the servlet class name and init param to be used is,


<servlet-name>myAction</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.test.jersey.action</param-value>
 </init-param>



4. Create a Java file inside the package com.test.jersey.action (or in its subpackage) which will map the request to corresponding action


package com.test.jersey.action;

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

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

@GET
@Produces("text/html")
public String getHelloMessage() {
return "<html><body>Hello World</body></html>";
}
}




5. Build the application and deploy it in any server. Now if you hit the url http://localhost:8080/hello you will get the message "Hello World". (The portnumber may differ based on your server setup).


If you need to accept input parameters from client, you can do so by using @PathParam annotation as below.


@GET
@Path("/user/{id}")
@Produces("text/html")
public String getHelloMessage(@PathParam("id") String name) {
return "<html><body>Hello "+name+"</body></html>";
}


If you hit the service using the url  http://localhost:8080/hello/user/ABC, it will return a text "Hello ABC".

Jersey is one of the best choice to develop simple light weight Webservices in Java.

No comments:

Post a Comment