com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段“消息”异常

Posted

技术标签:

【中文标题】com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段“消息”异常【英文标题】:com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "message" exception 【发布时间】:2020-10-30 00:53:40 【问题描述】:

请检查以下代码

import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.annotation.*;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder(
        "countryCode",
        "description",
        "message"
)

public class Country 

    @JsonProperty("countryCode")
    private String countryCode;
    @JsonProperty("description")
    private String description;
    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    @JsonProperty("countryCode")
    public String getCountryCode() 
        return countryCode;
    

    @JsonProperty("countryCode")
    public void setCountryCode(String countryCode) 
        this.countryCode = countryCode;
    

    @JsonProperty("description")
    public String getDescription() 
        return description;
    

    @JsonProperty("description")
    public void setDescription(String description) 
        this.description = description;
    

    @JsonAnyGetter
    public Map<String, Object> getAdditionalProperties() 
        return this.additionalProperties;
    

    @JsonAnySetter
    public void setAdditionalProperty(String name, Object value) 
        this.additionalProperties.put(name, value);
    


下面是堆栈跟踪

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "message" (class Dialog.Api.Testing.ReferenceData.models.Country), not marked as ignorable (2 known properties: "countryCode", "description"])
     at [Source: (String)""message":"Success","code":"0000","traceId":"abc1234567","origin":"/reference-data/countries","details":"https://confluence.coe.dialog.lk/display/CRMREW/Error+Codes","timestamp":"2020-07-09 15:28:38","data":"countries":["countryCode":"ALBVF","description":"ALBANIA","countryCode":"ALG","description":"ALGERIA","countryCode":"DZA","description":"ALGERIE","countryCode":"AND","description":"ANDORA","countryCode":"ANT","description":"ANTIGUA","countryCode":"ARG","description":"ARGENTINA""[truncated 14184 chars]; line: 1, column: 13] (through reference chain: Dialog.Api.Testing.ReferenceData.models.Country["message"])

【问题讨论】:

【参考方案1】:

在您的@JsonIncludeYou 中放置了 3 个属性。另一方面,您不会将 setter 和 getter 添加到消息属性。

重复: Jackson with JSON: Unrecognized field, not marked as ignorable

【讨论】:

【参考方案2】:

在您提到的课程中没有名称为 message 的属性。 您的代码中是否缺少某些内容? 如果您尝试将消息值放入映射中,则应删除 @JsonPropertyOrder 处的消息属性规范。

【讨论】:

以上是关于com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段“消息”异常的主要内容,如果未能解决你的问题,请参考以下文章