Spring MVC + REST service + XML Response


For returning the method response in XML form we need to use JAXB API.
JAXB API is inside the JDK-1.6 and we can use it directly by putting the @XMLRootElement as an annotation of the POJO returned object.

package com;

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

@XmlRootElement(name="response")
public class Response {

	String specVersion;
	Status status;
	public String getSpecVersion() {
		return specVersion;
	}
	public void setSpecVersion(String specVersion) {
		this.specVersion = specVersion;
	}
	public Status getStatus() {
		return status;
	}
	public void setStatus(Status status) {
		this.status = status;
	}
	
}

package com;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="status")
public class Status {

	int code;
	String mnemo;
	
	public int getCode() {
		return code;
	}
	public void setCode(int code) {
		this.code = code;
	}
	public String getMnemo() {
		return mnemo;
	}
	public void setMnemo(String mnemo) {
		this.mnemo = mnemo;
	}
	
}

while hit the URL , we will get the response in xml form like as follows:
http://localhost:8080/Mail/getVersion

<response>
<specVersion>34c</specVersion>
<status>
<code>100</code>
<mnemo>OK</mnemo>
</status>
</response>