如何进行springMVC xml与实体对象的互转,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
RequestMapping注解
@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>
关于如何进行springMVC xml与实体对象的互转问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。