【问题标题】:Java Spring JSON parse error: Cannot deserialize instance out of START_ARRAY tokenJava Spring JSON 解析错误:无法从 START_ARRAY 令牌中反序列化实例
【发布时间】:2019-04-28 07:03:46
【问题描述】:

我有一个带有这样的 restTemplate 调用的方法:

restTemplate.getForObject(apiUrl ,Someclass.class);

Someclass.class:

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Imp implements Serializable {
    @JsonProperty("Id")
    private String Id;

    @JsonProperty("ReportId")
    private String ReportId;

    @JsonProperty("Title")
    private String Title;

    @JsonProperty("Name")
    private String Name;

    @JsonProperty("Uri")
    private String Uri;

}

API 返回一个数组,我收到的 错误 是: org.springframework.web.client.RestClientException: 提取类型 [class ...] 和内容类型 [application/json;charset=utf-8] 的响应时出错;嵌套异常是 org.springframework.http.converter.HttpMessageNotReadableException:JSON 解析错误:无法从 START_ARRAY 令牌中反序列化 com... 的实例;嵌套异常是 com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of com... out of START_ARRAY token

我应该使用哪种 restTempalte 方法来获得正确的 api 响应?或者问题出在哪里?谢谢!

【问题讨论】:

    标签: java json spring rest jackson


    【解决方案1】:

    你说 API 返回一个数组。

    但是你的代码行restTemplate.getForObject(apiUrl ,Someclass.class); 仅适用于单个 Someclass 对象。

    您应该使用新的ParameterizedTypeReference<List<Someclass.class>> 以及交换方法。

    参考以下链接

    Get list of JSON objects with Spring RestTemplate

    【讨论】:

    猜你喜欢
    • 2020-04-03
    • 2020-04-18
    • 2020-07-14
    • 2020-09-23
    • 2020-06-16
    • 2016-06-05
    • 1970-01-01
    • 2021-01-12
    • 1970-01-01
    相关资源
    最近更新 更多