Spring MVC return XML data response

Spring MVC return XML data response

Below I will show you how you can return data in Spring MVC application XML format. I will show only main file here for details you could download project in bottom link.

  • Spring controller method:
@RequestMapping(value="/springAngularJS.web", 
            method=RequestMethod.GET,
            produces={"application/xml", "application/json"})
    public @ResponseBody Person  getPerson() {      
        Person person = new Person();
        person.setFirstName("Java");
        person.setLastName("Honk");
        return person;
  • Person.java class:
package com.javahonk.controller;

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

@XmlRootElement
public class Person {
    
    private String firstName;
    private String lastName;
    
    @XmlAttribute
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    @XmlAttribute
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }   
    
}

  • Data on web page:

Spring MVC return XML data response

 

  • For more information please read this spring tutorial

download Download Project:  SpringMVCAngularJS

 

Leave a Reply

Your email address will not be published. Required fields are marked *

I am not Robot *