In Spring 3, one of the feature of “mvc:annotation-driven“, is support for convert object to/from XML file, if JAXB is in project classpath.

In this tutorial, we show you how to convert a return object into XML format and return it back to user via Spring @MVC framework.

Technologies used :

  1. Spring 3.0.5.RELEASE
  2. JDK 1.6
  3. Eclipse 3.6
  4. Maven 3
JAXB is included in JDK6, so, you do not need to include JAXB library manually, as long as object is annotated with JAXB annotation, Spring will convert it into XML format automatically.

1. Project Dependencies

No extra dependencies, you need to include Spring MVC in your Maven pom.xml only.



		<!-- Spring 3 dependencies -->




2. Model + JAXB

A simple POJO model and annotated with JAXB annotation, later convert this object into XML output.

package com.mkyong.common.model;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "coffee")
public class Coffee {

	String name;
	int quanlity;

	public String getName() {
		return name;

	public void setName(String name) { = name;

	public int getQuanlity() {
		return quanlity;

	public void setQuanlity(int quanlity) {
		this.quanlity = quanlity;

	public Coffee(String name, int quanlity) { = name;
		this.quanlity = quanlity;

	public Coffee() {

3. Controller

Add “@ResponseBody” in the method return value, no much detail in the Spring documentation.

As i know, when Spring see

  1. Object annotated with JAXB
  2. JAXB library existed in classpath
  3. “mvc:annotation-driven” is enabled
  4. Return method annotated with @ResponseBody

It will handle the conversion automatically.

package com.mkyong.common.controller;

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.mkyong.common.model.Coffee;

public class XMLController {

	@RequestMapping(value="{name}", method = RequestMethod.GET)
	public @ResponseBody Coffee getCoffeeInXML(@PathVariable String name) {

		Coffee coffee = new Coffee(name, 100);
		return coffee;


4. mvc:annotation-driven

In one of your Spring configuration XML file, enable “mvc:annotation-driven“.

<beans xmlns=""
	xsi:schemaLocation="    ">

	<context:component-scan base-package="com.mkyong.common.controller" />

	<mvc:annotation-driven />

Alternatively, you can declares “spring-oxm.jar” dependency and include following MarshallingView, to handle the conversion. With this method, you don’t need annotate @ResponseBody in your method.

<beans ...>
	<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
	<bean id="xmlViewer" 
		  <bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
			<property name="classesToBeBound">

5. Demo

URL : http://localhost:8080/SpringMVC/rest/coffee/arabica

spring mvc and xml example demo

Download Source Code

Download it – (7 KB)


  1. Spring MVC and Rss example
  2. mvc-annotation-driven JavaDoc
  3. Jaxb2Marshaller JavaDoc
  4. ResponseBody.html JavaDoc