【问题标题】:Cannot deserialize instance of int[] out of START_OBJECT token无法从 START_OBJECT 令牌中反序列化 int[] 的实例
【发布时间】:2019-02-12 19:25:56
【问题描述】:

大家好,我想将 int 和 String 数组作为 RequestBody 发送: 这是json:

{
    "customUiModel": [1, 3, 5],
    "user": "user"
}

这是端点代码:

@RequestMapping(value = "/save", method = RequestMethod.POST)
      @ResponseStatus(HttpStatus.CREATED)
     public CustomUiModel createCustomUiObject(@RequestBody @Valid int[] customUiModel, String user) {

    return customAppService.saveCustom(customUiModel, user);
}

这是错误:

"message": "JSON 解析错误:无法反序列化 int[]out 实例 START_OBJECT 令牌;嵌套异常是 com.fasterxml.jackson.databind.exc.MismatchedInputException:不能 反序列化实例 ofint[] out of START_OBJECT token\n at [Source: (PushbackInputStream);行:1,列:1]","路径":"/custom/save"

我尝试使用 Array 而不是这个 int[] 但我遇到了同样的错误...

【问题讨论】:

标签: java arrays json spring-boot


【解决方案1】:

创建一个对象而不是int[], String 来保存它们,

public class Example {
    private int[] customUiModel;
    private String user;
}

并将控制器方法更改为,

public CustomUiModel createCustomUiObject(@RequestBody @Valid Example exe) {}

【讨论】:

  • json 应该保持不变?
  • @xmlParser 是的,customUiModeluser,它会根据同一个请求而改变吗?
  • 我现在会尝试,但我认为它不适用于相同的 json
  • 一切正常,先生。谢谢!
  • 有人能说出为什么对象类在这种情况下有效吗?提前致谢。
猜你喜欢
  • 2019-03-06
  • 2021-09-05
  • 2020-07-22
  • 2019-06-01
  • 2019-12-22
  • 2014-01-17
  • 2013-10-23
  • 2020-05-08
相关资源
最近更新 更多