簡體   English   中英

Jackson 反序列化錯誤:com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:無法識別

[英]Jackson deserialization error: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field

我在確實包含該字段的 model 上遇到了上述異常。 例外似乎也是零星的,這進一步引起了關注。 我當然可以用 class 包裝

JsonIgnoreProperties(ignoreUnknown = true)

但肯定不會想這樣做。

model如下:

public class OrderCommand {

    private int orderId;
    private String item;
    private int numberOfItems;
    private double price;
    private Payment payment;
    private String[] packages;
    private List<Shipment> shipment;
    private String orderStatus;

    public OrderCommand(){}

    public OrderCommand(String item, int numberOfItems, double price, OffsetDateTime timeStamp) {

        this.item = item;
        this.numberOfItems = numberOfItems;
        this.price = price;
        orderId = timeStamp.getNano();
    }

    public OrderCommand setOrderStatus(String orderStatus) {
        this.orderStatus = orderStatus;
        return this;
    }

    public String getOrderStatus(){
        return this.orderStatus;
    }

    public String getItem() {
        return item;
    }

    public void setItem(String item) {
        this.item = item;
    }

    public int getNumberOfItems() {
        return numberOfItems;
    }

    public void setNumberOfItems(int numberOfItems) {
        this.numberOfItems = numberOfItems;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public int getOrderId() {
        return orderId;
    }

    public void setOrderId(int orderId) {
        this.orderId = orderId;
    }

    public void setPackages(String[] packages) {
        this.packages = packages;
    }

    public String[] getPackages(){
        return packages;
    }

    public void setShipment(List<Shipment> shipment) {
        this.shipment = shipment;
    }

    public List<Shipment> getShipment(){
        return this.shipment;
    }

    public String toString(){
        return ReflectionToStringBuilder.toString(this);
    }

    public Payment getPayment() {
        return payment;
    }

    public OrderCommand setPayment(Payment payment) {
        this.payment = payment;
        return this;
    }
}

違規的 JSON 是:

{
    "item": "headphones",
    "price": 200.0,
    "orderId": 600000000,
    "payment": {
        "charge": 200.0,
        "paymentMethod": "VISA",
        "success": true,
        "failureReason": null,
        "accountNumber": "1234"
    },
    "packages": [
        "headphones.package0",
        "headphones.package1"
    ],
    "shipment": null,
    "orderStatus": "PAYMENT-RECEIVED",
    "numberOfItems": 2
}

如何防止這種情況發生並可靠地進行反序列化?

編輯 1:異常表明付款字段無法識別。 付款 class 是:

public class Payment {

    private double charge;
    private String paymentMethod;
    private boolean success;
    private String failureReason;
    private String accountNumber;

    public double getCharge() {
        return charge;
    }

    public Payment setCharge(double charge) {
        this.charge = charge;
        return this;
    }

    public String getPaymentMethod() {
        return paymentMethod;
    }

    public Payment setPaymentMethod(String paymentMethod) {
        this.paymentMethod = paymentMethod;
        return this;
    }

    public boolean getSuccess() {
        return success;
    }

    public Payment setSuccess(boolean success) {
        this.success = success;
        return this;
    }

    public String getFailureReason() {
        return failureReason;
    }

    public Payment setFailureReason(String failureReason) {
        this.failureReason = failureReason;
        return this;
    }

    public String getAccountNumber() {
        return accountNumber;
    }

    public Payment setAccountNumber(String accountNumber) {
        this.accountNumber = accountNumber;
        return this;
    }
}

編輯 2

完全例外如下:

Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field \"payment\" (class com.sailpoint.rss.rss_service.model.OrderCommand), not marked as ignorable (8 known properties: \"numberOfItems\", \"item\", \"orderId\", \"shipment\", \"packages\", \"orderProcessingTime\", \"orderProcessed\", \"price\"])
at [Source: (String)\"{\"item\":\"headphones\",\"price\":200.0,\"orderId\":36000000,\"payment\":{\"charge\":200.0,\"paymentMethod\":\"VISA\",\"success\":true,\"failureReason\":null,\"accountNumber\":\"1234\"},\"packages\":null,\"shipment\":null,\"orderStatus\":\"PAYMENT-RECEIVED\",\"numberOfItems\":2}\"; line: 1, column: 66] (through reference chain: com.sailpoint.rss.rss_service.model.OrderCommand[\"payment\"])
at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:61)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:823)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1153)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1589)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1567)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:294)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4014)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3005)
at io.zeebe.client.impl.ZeebeObjectMapper.fromJson(ZeebeObjectMapper.java:36)

也沒有提到,但偶爾發生。

我有經驗的猜測(在沒有更多細節的情況下)是問題出在orderStatuspayment上,因為setOrderStatussetPayment返回一個值而不是void

Jackson 關心這些事情,所以我建議使用@JsonSetter注釋這兩種方法。

問題也可能與付款有關,但由於您沒有說明無法反序列化哪個字段並且您也沒有發布Payment class 的源代碼,因此無法判斷。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM