gpt4 book ai didi

java - Jackson反序列化错误: com. fastxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段

转载 作者:行者123 更新时间:2023-12-02 00:28:40 24 4
gpt4 key购买 nike

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

来包装该类
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)

也没有提及,但偶尔发生。

最佳答案

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

Jackson 关心这些事情,因此我建议使用 @JsonSetter 注释这两个方法。

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

关于java - Jackson反序列化错误: com. fastxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58034249/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com