Jdk1.7.07
Maven 3.0.4
Spring Framework version 3.1.1.RELEASE
Spring Source Tools Suits(STS) 3.0
Download Source code here
To open the source code:
File --> Import --> Maven --> Existing Maven Projects
If you are new to Maven, please visit Maven Tutorial.
I am taking the task : development the Web Service REST using Spring Framework 3.1.1.RELEASE. This tutorial is the continuing of Spring 3.1 Web Service RESTful part01 - JSON and XML. In this phase, we will create the RESTful Web Service that return the list of employees in both JSON and XML.
What is REST?
Representational State Transfer (REST) is a style of software architecture for distributed hypermedia systems such as the World Wide Web. The term Representational State Transfer was introduced and defined in 2000 by Roy Fielding in his doctoral dissertation.
Source: http://en.wikipedia.org/wiki/Representational_State_Transfer
The Representational State Transfer (REST) style is an abstraction of the architectural elements within a distributed hypermedia system. REST ignores the details of component implementation and protocol syntax in order to focus on the roles of components, the constraints upon their interaction with other components, and their interpretation of significant data elements. It encompasses the fundamental constraints upon components, connectors, and data that define the basis of the Web architecture, and thus the essence of its behavior as a network-based application.
Source: Architectural Styles and the Design of Network-based Software Architectures, a dissertation of Roy Thomas Fielding,
REST vs SOAP
Unlike SOAP-based web services, there is no "official" standard for RESTful web services. This is because REST is an architecture, unlike SOAP, which is a protocol. Even though REST is not a standard, a RESTful implementation such as the Web can use standards like HTTP, URL, XML, PNG, etc.
Source: http://en.wikipedia.org/wiki/Representational_State_Transfer
What is REST in Spring 3?
For more information, please visit the following links:
- REST in Spring 3: @MVC
- REST in Spring 3: RestTemplate
The Web Service Provider
Our application will provide the information of employee list. The Service Provider will produce the XML and JSON data list.Domain Layer
We have domain object Employee and EmployeeList
Employee.java
package org.sample.mvc.domain;
import java.io.Serializable;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="employee")
public class Employee implements Serializable{
public Employee(){}
//Constructor for making sample data
public Employee(Integer id
, String firstName
, String lastName
, String userName
, String email
, String phone
, String image){
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.userName = userName;
this.email = email;
this.phone = phone;
this.image = image;
}
private static final long serialVersionUID = 1L;
private Integer id;
private String firstName;
private String lastName;
private String userName;
private String email;
private String phone;
private String image;
//getter and setter method
}
Employee is a simple POJO, and we've annotated the class name with
@XmlRootElement(name="employee"). Its purpose is to help JAXB (the one
responsible for marshalling/unmarshalling to XML) determine the root of
our POJO.EmployeeList.java
EmployeeList is another simple POJO. It's purpose is to wrap a list of Employee objects.
The Service Layer
The Service Layer will provide the data for Controller and then Controller responds the result for client.
EmployeeService.java
package org.sample.bl.service;
import org.sample.mvc.domain.EmployeeList;
public interface EmployeeService {
public EmployeeList getEmployeeByName(String name);
}
EmployeeServiceImpl.java
The Controller Layer The controller layer is the most important section of the provider service because here's where we define the RESTful services available to our clients.
package org.sample.mvc.restful;
import org.sample.bl.service.EmployeeService;
import org.sample.mvc.domain.Employee;
import org.sample.mvc.domain.EmployeeList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class EmployeeController {
private Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
EmployeeService employeeSevice;
/**
* Sample URL: http://localhost:8080/app0145/restful/emps/White1
*/
@RequestMapping("/emps/{name}")
public @ResponseBody EmployeeList getSearchEmployee(@PathVariable String name) {
logger.trace("Serving resource for name : " + name);
return employeeSevice.getEmployeeByName(name);
}
/**
@RequestMapping(value="/emps/{name}", method={RequestMethod.GET,RequestMethod.POST})
public @ResponseBody EmployeeList getSearchEmployee(@PathVariable String name) {
logger.trace("Serving resource for name : " + name);
return employeeSevice.getEmployeeByName(name);
}
@RequestMapping(value="/emps/{name}",
headers="Accept=application/xml")
public @ResponseBody EmployeeList getSearchEmployee(@PathVariable String name) {
logger.trace("Serving resource for name : " + name);
return employeeSevice.getEmployeeByName(name);
}
@RequestMapping(value="/emps/{name}",
headers="Accept=application/json")
public @ResponseBody EmployeeList getSearchEmployee(@PathVariable String name) {
logger.trace("Serving resource for name : " + name);
return employeeSevice.getEmployeeByName(name);
}
*/
}
Put the URL http://localhost:8080/app0145/restful/emps/White1 into the browser, the result will be as following
Configuration
Configuring REST support in Spring is almost the same Spring MVC. That's because REST in Spring are additional "features to Spring MVC itself" (See Rest in Spring 3: @MVC blog).Here are all the XML in the project
log4j.xml
servlet-context.xml
root-context.xml
web.xml
pom.xml
