首页 > 解决方案 > org.springframework.http.converter.HttpMessageNotReadableException:JSON 解析错误

问题描述

我有来自第 3 方的回复,如下所示

[
    {
        "url": "https://abc/10",
        "created": "2021-02-26 10:45:14",
        "status": "approved",
        "ref": "12452",
        "brand": "edr",
        "reason": "jkjkj"
    },
    {
        "url": "https://bvc/20",
        "created": "2021-02-26 10:43:18",
        "status": "rejected",
        "ref": "14562",
        "brand": "yghj",
        "reason": "asd",
    }
]

我们有一堂课。

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class GetDetails {

    private List<Detail> details = new ArrayList<>();

// getters, setters and toString() 
    
}

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class Detail {

    @JsonProperty(value = "url")
    private String url;

    @JsonProperty(value = "created")
    private Instant created;

    @JsonProperty(value = "status")
    private String status;

    @JsonProperty(value = "ref")
    private String ref;

    @JsonProperty(value = "brand")
    private String brand;

    @JsonProperty(value = "reason")
    private String reason;

//Getters, Setters and toString()

}

但是在转换时我们遇到了错误

错误 com.openbet.commons.sdk.common.util.RequestSenderImpl - HTTP 交换期间出现意外异常:org.springframework.http.converter.HttpMessageNotReadableException:JSON 解析错误:无法反序列com.document.GetDetails化 START_ARRAY 令牌的实例;嵌套异常是 com.fasterxml.jackson.databind.exc.MismatchedInputException:无法反序列com.document.GetDetails化 START_ARRAY 标记的实例

有人可以建议我在这里做错了什么吗?

标签: springjava-8jackson

解决方案


public ResponseEntity<?> getDetails(@RequestBody List<Detail> details){}

您的案例的方法签名示例。

json 中没有 details 属性

您正在尝试转换下面的示例,因此您无法转换它

{
  details : [
    {
        "url": "https://abc/10",
        "created": "2021-02-26 10:45:14",
        "status": "approved",
        "ref": "12452",
        "brand": "edr",
        "reason": "jkjkj"
    },
    {
        "url": "https://bvc/20",
        "created": "2021-02-26 10:43:18",
        "status": "rejected",
        "ref": "14562",
        "brand": "yghj",
        "reason": "asd",
    }
  ]
}

推荐阅读