JSON parse error: Cannot deserialize instance of `java.util.ArrayList` out of START_OBJECT token

描述:

  • 在前后端传递参数时,如果为JSON,后端使用@RequestBody对象类型接受数据,会出现 500/503错误。
  • 编译器同时报警告
    2020-07-02 16:57:19.774  WARN 16080 --- [io-7006-exec-10] .w.s.m.s.DefaultHandlerExceptionResolver :
    Resolved [org.springframework.http.converter.HttpMessageNotReadableException: 
    JSON parse error: Cannot deserialize instance of `java.util.ArrayList<com.xxx.xxx.model.order.OrderDetail>` out of START_OBJECT token; 
    nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: 
    Cannot deserialize instance of `java.util.ArrayList<com.xxx.xxx.model.order.OrderDetail>` out of START_OBJECT token
     at [Source: (PushbackInputStream); line: 1, column: 171] (through reference chain: com.xxx.xxx.model.order.OrderInfo["orderDetailList"])]
    

  • 警告内容:
    [org.springframework.http.converter.HttpMessageNotReadableException: 
    JSON parse error: Cannot deserialize instance of `java.util.ArrayList<com.xxx.xxx.model.order.OrderInfo>` 
    out of START_OBJECT token
    
    com.fasterxml.jackson.databind.exc.MismatchedInputException:
     Cannot deserialize instance of `java.util.ArrayList<com.xxx.xxx.model.order.OrderInfo>` out of START_OBJECT token
     at [Source: (PushbackInputStream)
    
  • 也就是说,在前后端发送数据时,出现JSON格式转换错误,从而访问不到后台接口。
  • 不添加 @RequestBody 虽然可以成功访问,但是无法获取到对象数据

解决

  • 不要使用对象类型接受,统一使用 Map 接收数据,就不会出现上述情况
  • @RequestBody Map<String, Object> data
    @PostMapping("/xxxx/xxxxx")
    public Result submitOrder(@RequestParam("xxxx") String tradeNo, @RequestBody Map<String, Object> data) {
         return Result.ok();
    }
    

版权声明:本文为weixin_44311939原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。