Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SpringMVC能否反序列化JSON,JSON可以是对象还是数组?_Java_Json_Spring_Spring Mvc_Jackson - Fatal编程技术网

Java SpringMVC能否反序列化JSON,JSON可以是对象还是数组?

Java SpringMVC能否反序列化JSON,JSON可以是对象还是数组?,java,json,spring,spring-mvc,jackson,Java,Json,Spring,Spring Mvc,Jackson,我让控制器在请求体中接收JSON,它可以是对象或对象数组。例如: { "id" : 1, "name" : "Nick", "surname" : "Cave" } 及 有没有办法强迫Spring将JSON反序列化为object,与单独的object类似? @RequestMapping(value = "/", method = RequestMethod.POST) public void postController(@RequestBody User user, ...)

我让控制器在请求体中接收JSON,它可以是对象或对象数组。例如:

{
  "id" : 1,
  "name" : "Nick",
  "surname" : "Cave"
}

有没有办法强迫Spring将JSON反序列化为object,与单独的object类似?

@RequestMapping(value = "/", method = RequestMethod.POST)
public void postController(@RequestBody User user, ...) {
   ...
}
如果没有,解析和验证此类消息的优雅方式是什么?

So 1)添加到maven:

<dependency>
  <groupId>org.codehaus.jackson</groupId>
  <artifactId>jackson-mapper-asl</artifactId>
  <version>1.9.13</version>
</dependency>

请看这里:或使用HttpMessageConverter:
<dependency>
  <groupId>org.codehaus.jackson</groupId>
  <artifactId>jackson-mapper-asl</artifactId>
  <version>1.9.13</version>
</dependency>
@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","");
}