1.pom.xml文件中添加xml的依赖包

<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.woodstox</groupId>
<artifactId>woodstox-core-asl</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>

2.User实体类

import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;

@JacksonXmlRootElement(localName = "user")
public class User {
private int id;
private String name;

public User() {

}
public User(int id, String name) {
this.id = id;
this.name = name;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}

3.UserControl

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("/xml")
public class UserControl {


// 输出xml
@RequestMapping(value = "/get", method = RequestMethod.GET, produces = { "application/xml" })
@ResponseBody
public User get(int id,String name) {
User user=new User();
user.setId(id);
user.setName(name);
return user;
}
}

4.启动sprintboot访问:http://localhost:8080/xml/get?id=6&name=link

springboot xml简单实例_springboot xml