簡體   English   中英

響應實體的Jersey Jackson JSON反序列化導致空白字段

[英]Jersey Jackson JSON deserialization of response entity results in blank fields

我遇到了一個問題。 當我的POJO被Jersey客戶通過電話反序列化時

jersey.api.ClientResponse#getEntity(OrderCostSummary.class)

響應json中的所有字段,即subTotal,shippingCost,tax和total均為空。

最初,我認為與Money類有關將是一個問題,即ObjectMapper不知道如何反序列化此類,但是我有其他api調用,它們返回帶有Money字段的響應實體,這些響應字段反序列化而沒有任何問題。

這是從服務器返回的JSON:

{ "orderCostSummary":{
  "subTotal":{
     "amount":"9.99",
     "currency":"GBP"
  },
  "shippingCost":{
   "money": {
     "amount":2.95,
     "currency":"GBP"
     }
  },
  "tax":{
     "amount":0,
     "currency":"GBP"
  },
  "total":{
     "amount":12.94,
     "currency":"GBP"
  }}}

POJO

public class OrderCostSummary {
private Money subTotal;
private Money shippingCost;
private Money tax;
private Money total;

/**
 * Instantiates a new Order cost summary.
 */
public OrderCostSummary() {
}

public Money getSubTotal() {
    return subTotal;
}

public void setSubTotal(Money subTotal) {
    this.subTotal = subTotal;
}

public Money getShippingCost() {
    return shippingCost;
}

public void setShippingCost(Money shippingCost) {
    this.shippingCost = shippingCost;
}

public Money getTax() {
    return tax;
}

public void setTax(Money tax) {
    this.tax = tax;
}

public Money getTotal() {
    return total;
}

public void setTotal(Money total) {
    this.total = total;
}

public class Money {
private BigDecimal amount;
private Currency currency;

我的錢POJO

public Money() { }

public Money(BigDecimal amount, Currency currency) {
    this.amount = amount;
    this.currency = currency;
}

public BigDecimal getAmount() {
    return amount;
}

public void setAmount(BigDecimal amount) {
    this.amount = amount;
}

public Currency getCurrency() {
    return currency;
}

public void setCurrency(Currency currency) {
    this.currency = currency;
}

也許如果您嘗試這樣做:

public class OrderCostSummary implements Serializable {
    @JsonDeserialize(as=Money.class)
    private Money subTotal;
    //And do the same with other fields.
    ...
}

請注意,我添加了“實現序列化”,這對於經序列化/反序列化的POJO更好

看來您的結構並不總是相同的:

這個結構:

{
   "orderCostSummary":{
      "subTotal":{
         "amount":"9.99",
         "currency":"GBP"
      },
      "shippingCost":{
         "amount":2.95,
         "currency":"GBP"
      },
      "tax":{
         "amount":0,
         "currency":"GBP"
      },
      "total":{
         "amount":12.94,
         "currency":"GBP"
      }
   }
}

與此結構不同:

{
   "orderCostSummary":{
      "subTotal":{
         "money":{
            "amount":"9.99",
            "currency":"GBP"
         }
      },
      "shippingCost":{
         "money":{
            "amount":2.95,
            "currency":"GBP"
         }
      },
      "tax":{
         "money":{
            "amount":0,
            "currency":"GBP"
         }
      },
      "total":{
         "money":{
            "amount":12.94,
            "currency":"GBP"
         }
      }
   }
}

答案是以下json應該已經通過調用反序列化了

jersey.api.ClientResponse#getEntity(ClassThatHoldsOrderCostSummary.class)

對象反序列化后,我可以調用

classThatHoldsOrderCostSummary.getOrderCostSummary()

暫無
暫無

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

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