요청하는 데이타 형식이 xml 인 경우가 있다.
이런 경우 해당 xml 을 오브젝트로 생성하여 @RequestBody 로 지정하면 xml의 각 Element 에 매핑할 수 있다.
1. xml 내용대로 오브젝트 생성하기
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<person>
<id>kim_9090</id>
<phone>01011112222</phone>
</person>
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="person")
public Class PersonModel{
@XmlElement(name="id")
private String id;
@XmlElement(name="phone")
private String phone;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
2. jsp 호출
<script>
$.ajax({
type:"post",
url: '/do/test/xml',
data: '<?xml version=\"1.0\" encoding=\"UTF-8\"?><person><id>kim_9090</id><phone>01011112222</phone></person>',
contentType:'application/xml;charset=utf-8',
async:true,
success:function(data, status, xhr){
console.log('성공');
},
error: function(jqXHR, errorText, errorThrown){
console.log('실패);
}
});
</script>
3. controller 지정
@RequestMapping("test/xml")
public ModelAndView testRequestXml( HttpServletRequest request, HttpServletResponse response, @RequestBody PersonModel model ) throws Exception {
System.out.println("id: " + model.getId());
System.out.println("phone: " + model.getPhone());
return new ModelAndView();
}
[출처] xml 을 object 로 매핑하기|작성자 감자쟁이