Archive

Archive for the ‘Restful’ Category

RESTful web service sample in Tomcat

September 5, 2010 Leave a comment

REST ( REpresentational State Transfer) is a software architecture for distributed system such as WWW ( World Wide Web ) . In REST every representations are referred as Resources. Each  Resources are identified by the URI( Uniform Resource Identifier ).

In Client Server architecture , representations of resources are exchaged directly over HTTP.

Actions performed by RESTful web services are mapped to standard HTTP methods (GET, POST, PUT,DELETE)

Libraries Used

Jersey jars:

asm-3.1.jar
jackson-core-asl-1.1.1.jar
jersey-client-1.3.jar
jersey-core-1.3.jar
jersey-json-1.3.jar
jersey-server-1.3.jar
jettison-1.1.jar
jsr311-api-1.1.1.jar

Jaxb jars:
activation.jar
jaxb1-impl.jar
jaxb-api.jar
jaxb-impl.jar
jaxb-xjc.jar
jsr173_1.0_api.jar

1. Create a entity class Employee.java

package com.room.ws.rest.sample;

import java.io.Serializable;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Employee implements Serializable{

	private static final long serialVersionUID = 1L;
	private Long id;
	private String name;
	private String dept;

	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getDept() {
		return dept;
	}
	public void setDept(String dept) {
		this.dept = dept;
	}

}

2. Create a resource class which has resource methods annotated by HTTP methods like @GET, @POST, @DELETE, @PUT

EmployeeResource.java

package com.room.ws.rest.sample;

import java.util.ArrayList;
import java.util.List;

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

import com.sun.jersey.spi.resource.Singleton;

@Path("emp")
@Singleton
public class EmployeeResource {

	private Employee emp1,emp2;;

	public EmployeeResource(){
		emp1=new Employee();
		emp1.setId(Long.valueOf(100));
		emp1.setName("EmployeeOne");
		emp1.setDept("ResearchDevelopment");

		emp2=new Employee();
		emp2.setId(Long.valueOf(200));
		emp2.setName("EmployeeTwo");
		emp2.setDept("InnovationQuality");
	}

	@GET
	@Produces("application/xml")
	public List<Employee> getEmployees(){
		List<Employee> empList=new ArrayList<Employee>();
		empList.add(emp1);
		empList.add(emp2);
		return empList;
	}

}

3. web.xml file contains the entry of this com.sun.jersey.spi.container.servlet.servletcontainer class to identify the REST web service requests.

<?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" id="WebApp_ID" version="2.5">
  <display-name>RESTSample</display-name>
  <servlet>
  	<servlet-name>RESTWS</servlet-name>
  	<servlet-class>com.sun.jersey.spi.container.servlet.servletcontainer</servlet-class>
  </servlet>
  <servlet-mapping>
  	<servlet-name>RESTWS</servlet-name>
  	<url-pattern>/services/*</url-pattern>
  </servlet-mapping>

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

Structure of REST Web project sample:

Deploy RESTful Service
Deploy the service into Tomcat.

Test RESTful Service
Using the URI the list of employees can be retrieved as XML
http://localhost:8080/RESTSample/services/emp

Response of service requested

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<Employees>
	<employee>
		<dept>ResearchDevelopment</dept>
		<id>100</id>
		<name>EmployeeOne</name>
	</employee>
	<employee>
		<dept>InnovationQuality</dept>
		<id>200</id>
		<name>EmployeeTwo</name>
	</employee>
</Employees>
Advertisements
Categories: Restful Tags: ,