【问题标题】:Bad deserialize with jackson用杰克逊反序列化不好
【发布时间】:2016-01-27 01:52:07
【问题描述】:

我尝试调用一个接受列表的 url。

发送的数据是

{"contactAvailableIdList":["1"],"contactIdList":["2","3"]}


Method on the server
@RequestMapping(value = "/lodgers/{lodgerId}/associate/dissociate/contact", method = RequestMethod.PUT)
public void associateLodgerAndContact(@PathVariable("lodgerId") Long lodgerId, @RequestBody @Valid final List<Long> contactIdList, @RequestBody @Valid final List<Long> contactAvailableIdList) {
    lodgerService.associateDissociateLodgerAndContact(lodgerId, contactIdList, contactAvailableIdList);
}

"{"timestamp":1445958336633,"status":400,"error":"Bad 请求","异常":"org.springframework.http.converter.HttpMessageNotReadableException","消息":"可以 未阅读文档:无法反序列化 java.util.ArrayList 的实例 [Source: 中的 START_OBJECT 令牌\n java.io.PushbackInputStream@5a1edae4;行:1,列:1];嵌套的 例外是 com.fasterxml.jackson.databind.JsonMappingException: Can 不在 START_OBJECT 中反序列化 java.util.ArrayList 的实例 令牌\n 在 [来源:java.io.PushbackInputStream@5a1edae4;线:1, 列:1]","路径":"/rest/lodgers/1/associate/dissociate/contact"}"

【问题讨论】:

    标签: spring jackson spring-rest


    【解决方案1】:

    我不确定,您是否可以一次接受更多请求体。

    标准的方式是上课

    public class Contact{
      List<Long> contactIdList;
      List<Long> contactAvailableIdList;
    }
    

    然后接受它为

    @RequestBody @Valid Contact contact
    

    此外,您正在接收字符串数组,并尝试将它们保存到 Long 数组中。我也不确定,如果杰克逊在这种情况下隐式尝试将数组的字符串元素转换为 long。

    【讨论】:

    • 我改用一个类,但是两个列表都是空的
    • 尝试使用 List 而不是 List
    • Jackson 会在必要时将 JSON 字符串强制转换为整数,除非配置设置明确阻止。
    猜你喜欢
    • 1970-01-01
    • 2015-04-07
    • 2013-11-20
    • 1970-01-01
    • 1970-01-01
    • 2012-11-19
    • 2016-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多