In some previous post we saw how we can return Json objects from Spring MVC controllers. Now we will see examples of how we can use the @ResponseBody to return an XML representation of an object from the Controller. We will need to add the @XmlRootElement and @XmlElement(optional) to the Model class that we need to return.
We will see 2 examples- one returning a Single Employee Object and other returning a list of Employee objects.
Lets see Spring MVC Xml Response examples.
1. Employee Model Class
We need 2 model classes – Employee Object and EmployeeList Object
Employee.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
package com.kscodes.sampleproject.model; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "employee") public class Employee { private String id; private String firstName; private String lastName; private int age; private double salary; public Employee() { super(); } public Employee(String id, String firstName, String lastName, int age, double salary) { super(); this.id = id; this.firstName = firstName; this.lastName = lastName; this.age = age; this.salary = salary; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } } |
EmployeeList.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.kscodes.sampleproject.model; import java.util.List; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "employee-list") public class EmployeeList { private List<Employee> employees; public List<Employee> getEmployees() { return employees; } public void setEmployees(List<Employee> employees) { this.employees = employees; } } |
2. Controller
In the Controller we have 2 methods. One to get a Single Employee Object and Other to return a List of Employees.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
package com.kscodes.sampleproject.controller; import java.util.ArrayList; import java.util.List; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import com.kscodes.sampleproject.model.Employee; import com.kscodes.sampleproject.model.EmployeeList; @Controller @RequestMapping(value = "/xml") public class XmlResponseController { @RequestMapping(value = "/employee", method = RequestMethod.GET) public @ResponseBody Employee getEmployeeData() { Employee employee = new Employee("001", "Steve", "Rob", 30, 4500); return employee; } @RequestMapping(value = "/employeeList", method = RequestMethod.GET) public @ResponseBody EmployeeList getEmployeeListData() { List<Employee> employees = new ArrayList<Employee>(); Employee e1 = new Employee("001", "Steve", "Rob", 30, 4500); Employee e2 = new Employee("001", "Steve", "Rob", 30, 4500); Employee e3 = new Employee("001", "Steve", "Rob", 30, 4500); employees.add(e1); employees.add(e2); employees.add(e3); EmployeeList employeeList = new EmployeeList(); employeeList.setEmployees(employees); return employeeList; } } |
Please Note : If we do not add the
@XmlRootElement the retunr object will not be a XML , but rather be a simple text.