第一步:pom文件添加依赖
<dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-xml</artifactId> </dependency>
第二步:将返回的对象添加相应的注解
package com.example.domain; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class Student { private String name; private int age; private char sex; @XmlElement public String getName() { return name; } public void setName(String name) { this.name = name; } @XmlElement public int getAge() { return age; } public void setAge(int age) { this.age = age; } @XmlElement public char getSex() { return sex; } public void setSex(char sex) { this.sex = sex; } }
第三步:控制器方法
/** * 测试返回xml格式数据 * 第一步: * @return */ @ResponseBody @RequestMapping( value="xml", produces= { MediaType.APPLICATION_XML_VALUE } ) public Student sayHelloXml() { Student stu = new Student(); stu.setName("Xml"); stu.setAge(10); stu.setSex('女'); return stu; }
结果显示: