Spring MVC - XML response with @ResponseBody annotation (Spring + XML)

Updated for Spring 5+

In today's discussion we will see how to return a XML response from a Spring MVC Controller. @ResponseBody annotation does this job done for us, all we need to do is adding @ResponseBody annotation in front of controller and annotate the object we want to return in xml form. Let's see the functioning of @ResponseBody annotation and how it helps in returning an java object in XML form.

Expected Output

Our objective for today's discussion is clear, we will try to return a Java Object and a list of java objects as xml response to the browser. See screen shots below how this will look like:

Single Java object as xml response(Student.java)

List of Java objects as xml response(List )

Return a Java object as xml response from Spring MVC controller

To return an java object in xml form from an spring objects requires two simple configurations:
1) Annotate Object Class
2) Add @ResponseBody annotation to the controller's method

1) Annotate Object Class

This is a simple Java class (Student) whose object will be returned in xml form, from the controller. @XmlRootElement defines the root element of xml structure, by default it takes class name as root element. Optionally we can give it s "name" parameter if we want some other name as xm root element. @XmlElement defines internal xml nodes, 'name' attribute applies here as well, as same as with @XmlRootElement.
package com.beingjavaguys.domain;  
import javax.xml.bind.annotation.XmlRootElement;  
import javax.xml.bind.annotation.XmlElement;  
@XmlRootElement(name = "student")  
public class Student {  
 private int id;  
 private String firstName;  
 private String lastName;  
 private String email;  
 private String phone;  
 protected Student() {  
 public Student(int id, String firstName, String lastName, String email,  
   String phone) {  
  this.id = id;  
  this.firstName = firstName;  
  this.lastName = lastName;  
  this.email = email;  
  this.phone = phone;  
 public int getId() {  
  return id;  
 public void setId(int 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 String getEmail() {  
  return email;  
 public void setEmail(String email) {  
  this.email = email;  
 public String getPhone() {  
  return phone;  
 public void setPhone(String phone) {  
  this.phone = phone;  

2) Add @ResponseBody annotation to the controller's method

Second thing we need to do is to use '@ResponseBody' annotation against the controller's method. This will make spring understand that method return value should be bound to the web response body.
package com.beingjavaguys.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.ResponseBody;  
import com.beingjavaguys.domain.Student;  
import com.beingjavaguys.domain.StudentList;  
public class DataController {  
 public @ResponseBody  
 Student getStudent() {  
  return new Student(23, "Meghna", "Naidu", "meghna@gmail.com",  
 public @ResponseBody  
 StudentList getStudentList() {  
  List<student> studentList = new ArrayList<student>();  
  studentList.add(new Student(3, "Robert", "Parera", "robert@gmail.com",  
  studentList.add(new Student(93, "Andrew", "Strauss",  
    "andrew@gmail.com", "8978767878"));  
  studentList.add(new Student(239, "Eddy", "Knight", "knight@gmail.com",  
  return new StudentList(studentList);  

Return a List of Java object as xml response from Spring MVC controller

We just saw how to return an java object in xml form, that was quit simple because we have full control to 'Student' class and hence we could annotate the class easily. In case we want to return a list of objects say 'ArrayList', but we dont have ArrayList class's code to annotate it. So its a bit tricky; we have to make an other class having a list of objects and the object of that annotated class will represent the whole list and will be returned as a list of xml nodes.

package com.beingjavaguys.domain;  
import java.util.List;  
import javax.xml.bind.annotation.XmlRootElement;  
@XmlRootElement(name = "student-list")  
public class StudentList {  
 private List<student> studentList;  
 protected StudentList() {  
 public StudentList(List<student> studentList) {  
  this.studentList = studentList;  
 @javax.xml.bind.annotation.XmlElement(name = "student")  
 public List<student> getStudentList() {  
  return studentList;  

That's all we need to know about @ResponseBody annotation and how to return xml response from Spring Controller. Lets now add all other useful files to our project to make it working.

Dependencies required to return xml response from Spring Controller.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<name>SampleSpringMaven Maven Webapp</name>








Spring configuration file

This is simple spring configuration file, we have added an entry for our controller's location. Dont forget to add '<mvc:annotation-driven />' this is very important for @ResponseBody annotation to work.
<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
 xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"  
 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd  
  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd  
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">  
 <context:component-scan base-package="com.beingjavaguys.controller" />  
 <mvc:annotation-driven />  

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
 <display-name>Sample Spring Maven Project</display-name>  

This is all we have added all required files to the project, lets see the overall project structure at once now. Project Structure

If everything goes right and you have configured everything well, run the projct on server and hit following two URL's in the browser.

1)Java Object as xml response : http://localhost:8080/SampleSpringMaven/student.xml
2)Java object's list as xml response: http://localhost:8080/SampleSpringMaven/studentlist.xml

In upcoming blogs we will see more about Spring, Hibernate, Java and Other opensource technologies.

Download source code from: GitHub