【问题标题】:Can't deserialize json into map of objects after REST request [duplicate]REST请求后无法将json反序列化为对象映射[重复]
【发布时间】:2018-01-23 00:39:18
【问题描述】:

我得到了服务器响应我的 json:

    {
    "clients": {
        "0": {
            "id": 12370691,
            "fio": ""
        },
        "2": {
            "id": 12384782,
            "fio": "name2"
        },
        "3": {
            "id": 12389624,
            "fio": "Name3"

        }
    }
}

由于 jsons,我得到了 pojos。

第一个 Pojo 包含对象映射

    @Data
public class GetClientsResponse {
    @JsonProperty("clients")
    Map<String, ClientResponse> clientMap;

    public List<ClientResponse> getListOfValues() {
        if (Objects.nonNull(this.clientMap)) {
            return this.clientMap.entrySet()
                    .stream()
                    .map(entry -> entry.getValue())
                    .collect(Collectors.toList());
        }
        return Collections.emptyList();
    }
}

和ClientResponse类

    @Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class ClientResponse {
    @JsonProperty("id")
    private int clientId;
    @JsonProperty("fio")
    private String fullName;
}

我得到了下一个错误:

出现意外错误(类型=错误请求,状态=400)。 JSON 解析错误:无法反序列化 java.util.LinkedHashMap 的实例 超出 START_ARRAY 令牌;嵌套异常是 com.fasterxml.jackson.databind.JsonMappingException:不能 从 START_ARRAY 中反序列化 java.util.LinkedHashMap 的实例 [来源:java.io.PushbackInputStream@2a8f551b;线:1, 列:12](通过参考链: com.lineup.flatsender.model.dto.response.avers.getclinets.GetClientsResponse["clients"]) 块引用

谁能解释一下是什么问题?因为它在 1 个月前有效,我没有任何问题。 我正在使用 import org.springframework.web.client.RestOperations;

【问题讨论】:

    标签: java json rest


    【解决方案1】:

    服务最近有变化吗? 400 响应表明服务器无法理解您的请求。

    你能发布你返回的实际 JSON 吗?该错误表明您正在使用 JSON 数组返回某些内容,但您发布的内容未显示数组

    【讨论】:

      【解决方案2】:

      JsonMappingException: out of START_ARRAY token 异常是 Jackson 对象映射器的 thrown,因为它期待一个 Object {},而它在响应中找到了一个 Array [{}]

      因此可能是来自 web 服务的响应发生了变化,您的客户数据现在以数组而不是对象的形式返回。

      【讨论】:

      • 请发布答案被否决的原因,以便我改进。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-02
      • 2011-12-23
      • 1970-01-01
      • 2020-06-24
      相关资源
      最近更新 更多