728x90
반응형

요청하는 데이타 형식이 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();

728x90
반응형
블로그 이미지

nineDeveloper

안녕하세요 현직 개발자 입니다 ~ 빠르게 변화하는 세상에 뒤쳐지지 않도록 우리모두 열심히 공부합시다 ~! 개발공부는 넘나 재미있는 것~!

,