Java and J2EE Tutorials, Jsp and Servlet Tutorials, Spring MVC, Solr, XML, JSON Examples, Hibernate & Struts 2 Hello World projects

Friday, 30 May 2014

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

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(

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) { = id;
  this.firstName = firstName;
  this.lastName = lastName; = email; = phone;

 public int getId() {
  return id;

 public void setId(int 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) { = email;

 public String getPhone() {
  return phone;

 public void setPhone(String 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", "",

 public @ResponseBody
 StudentList getStudentList() {
  List studentList = new ArrayList();
  studentList.add(new Student(3, "Robert", "Parera", "",
  studentList.add(new Student(93, "Andrew", "Strauss",
    "", "8978767878"));
  studentList.add(new Student(239, "Eddy", "Knight", "",

  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 studentList;

 protected StudentList() {

 public StudentList(List studentList) {
  this.studentList = studentList;

 @javax.xml.bind.annotation.XmlElement(name = "student")
 public List 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="" xmlns:xsi=""
 <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=""
 xmlns:xsi="" xmlns:context=""
 xmlns:util="" xmlns:mvc=""

 <context:component-scan base-package="com.beingjavaguys.controller" />
 <mvc:annotation-driven />



<web-app xmlns="" xmlns:xsi=""

 <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

You can also download full example project from the link given below in BLUE STRIP. In upcoming blogs we will see more about Spring, Hibernate, Java and Other opensource technologies.

Thanks for reading !
Being Java Guys Team

Download " XML response with @ResponseBody annotation Example Project" from "SkyDrive"

1 comment:

  1. thank you very much, very nice tutorial #spring #xml


Like Us on Facebook

Like Us On Google+


Skype: neel4soft