Spring MVC Xml Response Example

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


2. Controller

In the Controller we have 2 methods. One to get a Single Employee Object and Other to return a List of Employees.

Please Note : If we do not add the @XmlRootElement the retunr object will not be a XML , but rather be a simple text.

3. Output

Spring MVC Sample | Spring MVC Xml Response with Example

Spring MVC Xml Response with Example | Spring MVC Sample

4. Download Full Code –SpringMVCXmlGet.zip

It's only fair to share...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn