순수한 공상과학연구소

출처: https://jhw1308.tistory.com/21

 

Spring 3.1 @RequestBody 유의점

레거시 프로젝트를 다루는 일이 많아지면서 전에 직면하지 못했던 문제들을 해결해야할 경우가 종종 생기게 됐다 당연하게 사용해왔던 @RequestBody 역시나 마찬가지 -_-.. Spring 3.0 -> 3.1로 버전이

jhw1308.tistory.com

 

요약 

@RequestMapping(value = "/ajax/admin/documentTypeList.do", method = RequestMethod.POST)
@ResponseBody
public ResponseEntity changeDocumentStatus(@RequestBody List<DocumentTypeVO> list) {
// 만약 json 포멧이 list 형태이고 이를 @RequestBody 로 받으려면 Dto을 써서 받는데 

// 그냥 받으면 안된다. 이유는 @RequestBody 가 json 에서 LinkedHashMap형식으로받기때문이다

// 그래서 LinkedHashMap 을 List 로 바꿔야한다.  ObjectMapper mapper = new ObjectMapper(); 을 쓴다. 


ObjectMapper mapper = new ObjectMapper();
List<DocumentTypeVO> al = mapper.convertValue(list, new TypeReference<List<DocumentTypeVO>>() {});

String res = masterService.documentUseYNChange(al);

if ("success".equals(res)) {
return new ResponseEntity(res, HttpStatus.OK);
} else {
return new ResponseEntity(res, HttpStatus.BAD_REQUEST);
}
}

출처: https://jhw1308.tistory.com/21 [귀찮지만 만들어보자]