@PostMapping(value = "/testXmlRequest",
produces = "application/xml; charset=UTF-8",
consumes = "application/xml; charset=UTF-8")
public UserDto testXmlRequest(@RequestBody UserDto dto){
dto.chgName("2342424sdfsdfsdf");
return dto;
}
produces 设置响应的数据格式。最终httpMessageConverter根据produces的响应media-type来选择对应的转换类。
consumes 设置请求的数据格式最终httpMessageConverter根据consumes的响应media-type来选择对应的转换类。
实体类
@XmlRootElement(name = "xml")
public class UserDto {
@XmlElement(name = "id")
private Integer id;
@XmlElement(name = "name")
private String name;
@XmlElement(name = "sex")
private Integer sex;
public UserDto() {
}
public UserDto(Integer id, String name, Integer sex) {
this.id = id;
this.name = name;
this.sex = sex;
}
public void chgName(String name){
this.name = name;
}
}
javax.xml.bind.annotation.*的相关注解。会自动封装和解析。注意:对应的field不能有getter和setter,不然会报错。
因此,通常对于属性的设置,创建对应的builder类就可以了。
例子
headers:Content-Type=application/xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xml>
<id>12131</id>
<name>test</name>
<sex>1</sex>
</xml>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xml>
<id>12131</id>
<name>2342424sdfsdfsdf</name>
<sex>1</sex>
</xml>
原文地址:http://blog.51cto.com/881206524/2119028