Jackson UnrecognizedPropertyException

sre*_*ree 5 java spring json jackson

我正在尝试将 JsonString 转换为 Java 对象。

JSON 对象

{
"action": "added",
"data": {
    "Quote": {
        "TotalDiscountsAmount": 0,
        "Id": "test123"
    },
    "Owner": {
        "Username": "00000000",
        "Id": "00000000"
    },
    "Discount_Amount__c": 0,
    "Base_List_Price__c": 574.88,
    "TotalList": 574.88,
    "Id": "000000",
    "ExtendedTotalList": 574.88,
    "BaseListPrice": 474.88
}
}
Run Code Online (Sandbox Code Playgroud)

我的 POJO:

AddtocartJson.java

package com.product.json;

import javax.annotation.Generated;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("org.jsonschema2pojo")
@JsonPropertyOrder({
  "action",
  "data"
})
public class AddToCartJson {
    @JsonProperty("action")
    private String action;
    @JsonProperty("data")
    private Data data;

    @JsonProperty("action")
    public String getAction() {
        return action;
    }

    @JsonProperty("action")
    public void setAction(String action) {
        this.action = action;
    }

    @JsonProperty("data")
    public Data getData() {
        return data;
    }

    @JsonProperty("data")
    public void setData(Data data) {
        this.data = data;
    }
}
Run Code Online (Sandbox Code Playgroud)

数据.java

package com.product.json;

import javax.annotation.Generated;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("org.jsonschema2pojo")
@JsonPropertyOrder({
  "Quote",
  "Owner",
  "Discount_Amount__c",
  "Base_List_Price__c",
  "TotalList",
  "Id",
  "ExtendedTotalList",
  "BaseListPrice"
})
public class Data {

    @JsonProperty("Quote")
    private com.product.json.Quote Quote;
    @JsonProperty("Owner")
    private com.product.json.Owner Owner;
    @JsonProperty("Discount_Amount__c")
    private Integer DiscountAmountC;
    @JsonProperty("Base_List_Price__c")
    private Double BaseListPriceC;
    @JsonProperty("TotalList")
    private Double TotalList;
    @JsonProperty("Id")
    private String Id;
    @JsonProperty("ExtendedTotalList")
    private Double ExtendedTotalList;
    @JsonProperty("BaseListPrice")
    private Double BaseListPrice;

    @JsonProperty("Quote")
    public com.product.json.Quote getQuote() {
        return Quote;
    }

    @JsonProperty("Quote")
    public void setQuote(com.product.json.Quote Quote) {
        this.Quote = Quote;
    }

    @JsonProperty("Owner")
    public com.product.json.Owner getOwner() {
        return Owner;
    }

    @JsonProperty("Owner")
    public void setOwner(com.product.json.Owner Owner) {
        this.Owner = Owner;
    }

    @JsonProperty("Discount_Amount__c")
    public Integer getDiscountAmountC() {
        return DiscountAmountC;
    }

    @JsonProperty("Discount_Amount__c")
    public void setDiscountAmountC(Integer DiscountAmountC) {
        this.DiscountAmountC = DiscountAmountC;
    }

    @JsonProperty("Base_List_Price__c")
    public Double getBaseListPriceC() {
        return BaseListPriceC;
    }

    @JsonProperty("Base_List_Price__c")
    public void setBaseListPriceC(Double BaseListPriceC) {
        this.BaseListPriceC = BaseListPriceC;
    }

    @JsonProperty("TotalList")
    public Double getTotalList() {
        return TotalList;
    }

    @JsonProperty("TotalList")
    public void setTotalList(Double TotalList) {
        this.TotalList = TotalList;
    }

    @JsonProperty("Id")
    public String getId() {
        return Id;
    }

    @JsonProperty("Id")
    public void setId(String Id) {
        this.Id = Id;
    }

    @JsonProperty("ExtendedTotalList")
    public Double getExtendedTotalList() {
        return ExtendedTotalList;
    }

    @JsonProperty("ExtendedTotalList")
    public void setExtendedTotalList(Double ExtendedTotalList) {
        this.ExtendedTotalList = ExtendedTotalList;
    }

    @JsonProperty("BaseListPrice")
    public Double getBaseListPrice() {
        return BaseListPrice;
    }

    @JsonProperty("BaseListPrice")
    public void setBaseListPrice(Double BaseListPrice) {
        this.BaseListPrice = BaseListPrice;
    }
}
Run Code Online (Sandbox Code Playgroud)

报价.java

package com.product.json;

import javax.annotation.Generated;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("org.jsonschema2pojo")
@JsonPropertyOrder({
  "TotalDiscountsAmount",
  "Id"
})
public class Quote {
    @JsonProperty("TotalDiscountsAmount")
    private Integer TotalDiscountsAmount;
    @JsonProperty("Id")
    private String Id;

    @JsonProperty("TotalDiscountsAmount")
    public Integer getTotalDiscountsAmount() {
        return TotalDiscountsAmount;
    }

    @JsonProperty("TotalDiscountsAmount")
    public void setTotalDiscountsAmount(Integer TotalDiscountsAmount) {
        this.TotalDiscountsAmount = TotalDiscountsAmount;
    }

    @JsonProperty("Id")
    public String getId() {
        return Id;
    }

    @JsonProperty("Id")
    public void setId(String Id) {
        this.Id = Id;
    }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试使用映射器将 JSON 转换为 Java 类时,它会抛出一个UnrecognizedPropertyException. 即使我有一个Quote对象,映射器也无法识别该对象,但似乎很喜欢。

org.codehaus.jackson.map.exc.UnrecognizedPropertyException:无法识别的字段“Quote”(类 com.product.json.Data),未在 [来源:java.io.StringReader@11547748; 行:1,列:36](通过参考链:com.product.json.AddToCartJson["data"]-> com.product.json.Data["Quote"])在 org.codehaus.jackson.map.exc .UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:53) 在 org.codehaus.jackson.map.deser.StdDeserializationContext.unknownFieldException(StdDeserializationContext.java:267) 在 org.codehaus.jackson.map.deser.std.StdDeserializer(StdDeserializer)。 .java:673)

我怎样才能解决这个问题?

Tom*_*Tom 7

这里的问题是 OP 混合了两个不同的 Jackson 版本:

  • 1.9 来自org.codehaus(org.codehaus.jackson.map.exc.UnrecognizedPropertyException表明)
  • 2.x from com.fasterxml(在导入中可见import com.fasterxml.jackson.annotation.

正如sam在评论中指出的那样,OP 应该只使用一个版本。要么使用 2.x 进行反序列化,要么使用旧的 1.9 版本中的注释。但建议使用来自 的最新版本com.fasterxml,而不是旧版本。

  • 足够公平,但埋藏在某处,因为某些过时的依赖项的依赖项并不是最容易缓解的。 (2认同)

Ole*_*syn 5

您可能想通过使用 Jackson 的类级注释来忽略未定义的属性:

@JsonIgnoreProperties