<beans xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemalocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<mvc:annotation-driven></mvc:annotation-driven>
<context:component-scan base-package="com.dineshonjava.web">
</context:component-scan>
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<entry key="html" value="text/html"></entry>
<entry key="json" value="application/json"></entry>
<entry key="xml" value="application/xml"></entry>
</map>
</property>
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView">
</property>
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</list>
</property>
</bean>
</beans>
Step 3: Add model classes: There are two model classes Employee.java and Employees.javapackage com.dineshonjava.web.model;
import java.util.Collection;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
/**
* @author Dinesh Rajput
*
*/
@XmlRootElement(name="employees")
@XmlAccessorType(XmlAccessType.NONE)
public class Employees {
@XmlElement(name="employee")
private Collection<Employee> employees;
public Collection<Employee> getEmployees() {
return employees;
}
public void setEmployees(Collection<Employee> employees) {
this.employees = employees;
}
}
Now write Employee.javapackage com.dineshonjava.web.model;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
/**
* @author Dinesh Rajput
*
*/
@XmlRootElement(name="employees")
@XmlAccessorType(XmlAccessType.NONE)
public class Employee {
@XmlElement(name="empId")
private Integer id;
@XmlElement(name="empName")
private String name;
@XmlElement(name="empAge")
private Integer age;
@XmlElement(name="empSalary")
private Long salary;
@XmlElement(name="empAddress")
private String address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Long getSalary() {
return salary;
}
public void setSalary(Long salary) {
this.salary = salary;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
Step 4: Write the controller - WebServiceController.javapackage com.dineshonjava.web.controller;
import java.util.ArrayList;
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;
import com.dineshonjava.web.model.Employee;
import com.dineshonjava.web.model.Employees;
/**
* @author Dinesh Rajput
*
*/
@Controller
@RequestMapping("/employees")
public class WebServiceController {
@RequestMapping(method = RequestMethod.GET, value="/{id}", headers="Accept=*/*")
public @ResponseBody Employee getEmployeeById(@PathVariable String id){
Employee employee = new Employee();
employee.setId(1);
employee.setSalary(50000l);
employee.setName("Dinesh Rajput");
employee.setAge(26);
employee.setAddress("Sector 49-Noida");
return employee;
}
@RequestMapping(method = RequestMethod.GET, headers="Accept=*/*")
public @ResponseBody Employees getEmployees(){
Employees employees = new Employees();
Employee employee1 = new Employee();
employee1.setId(2);
employee1.setSalary(50000l);
employee1.setName("Dinesh Rajput");
employee1.setAge(26);
employee1.setAddress("Sector 49- Noida");
Employee employee2 = new Employee();
employee2.setId(3);
employee2.setSalary(20000l);
employee2.setName("Anamika Rajput");
employee2.setAge(26);
employee2.setAddress("Sector 49- Noida");
employees.setEmployees(new ArrayList<Employee>());
employees.getEmployees().add(employee1);
employees.getEmployees().add(employee2);
return employees;
}
}
web.xml<web-app version="2.5" 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_2_5.xsd">
<servlet>
<servlet-name>sdnext</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name><param-value>/WEB-INF/config/sdnext-servlet.xml</param-value></init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>sdnext</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
Now lets deploy the application on tomcat and hit the URL on any REST client. I am using RESTClient. This is a firefox plugin for testing the RESTful webservices.

Labels: Spring MVC, Spring3.0