【问题标题】:Jackson deserialization error: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field杰克逊反序列化错误:com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段
【发布时间】:2019-09-20 19:35:17
【问题描述】:

我在包含该字段的模型上遇到上述异常。该异常似乎也是零星的,这进一步引起了关注。我当然可以用

包装类
JsonIgnoreProperties(ignoreUnknown = true)

但肯定不想这样做。

型号如下:

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: 异常表示付款字段未被识别。支付类为:

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)

没有提及但偶尔发生。

【问题讨论】:

  • 如果你不想使用这个JsonIgnoreProperties(ignoreUnknown = true) 然后写你的自定义反序列化器
  • 杰克逊抱怨的领域是什么?是orderStatus吗?
  • @Deadpool 我想你误解了这个问题。我相信 OP 认为 bean 具有 JSON 中的所有属性,因此不应抛出 UnrecognizedPropertyException
  • 他从来没有提到他有属性@Paul的bean所以我不确定他需要什么
  • @Deadpool 他试图将 JSON 反序列化为 OrderCommand 的实例。

标签: java jackson deserialization


【解决方案1】:

我有经验的猜测(在没有更多细节的情况下)是问题出在orderStatuspayment,因为setOrderStatussetPayment 返回一个值而不是void

Jackson 关心这些事情,所以我建议用 @JsonSetter 注释这两种方法。

问题也可能与付款有关,但由于您没有说明无法反序列化哪个字段并且您也没有发布 Payment 类的源代码,因此无法判断。

【讨论】:

  • 我的错,你是对的,它在抱怨支付领域。
  • 您是否尝试将@JsonSetter 添加到public OrderCommand setPayment(Payment payment)
  • 是的,我做到了。我什至更改了所有设置器的签名以返回 void 但问题仍然存在。也很零星。
猜你喜欢
  • 2012-01-14
  • 1970-01-01
  • 2019-08-26
  • 1970-01-01
  • 2013-07-10
  • 2017-04-25
  • 1970-01-01
  • 2019-08-04
  • 2017-12-26
相关资源
最近更新 更多