객체 또는 객체 배열이 될 수있는 요청 본문에 JSON을 수신하는 컨트롤러가 있습니다. 예를 들면 :
{
"id" : 1,
"name" : "Nick",
"surname" : "Cave"
}
과
[
{
"id" : 1,
"name" : "Nick",
"surname" : "Cave"
},
{
"id" : 2,
"name" : "Jack",
"surname" : "White"
}
]
Spring이 객체 단독과 비슷한 방식으로 JSON을 객체로 deserialize하도록 강제하는 방법이 있습니까?
@RequestMapping(value = "/", method = RequestMethod.POST)
public void postController(@RequestBody User user, ...) {
...
}
그렇지 않은 경우 이러한 종류의 메시지를 구문 분석하고 유효성을 검사하는 우아한 방법은 무엇입니까?
그래서 1) maven에 추가하십시오.
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
2)
@RequestMapping(value = "/addPerson",
method = RequestMethod.POST,
headers = {"Content-type=application/json"})
@ResponseBody
public JsonResponse addPerson(@RequestBody Person person) {
logger.debug(person.toString());
return new JsonResponse("OK","");
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다