泽西杰克逊JSON反序实体的反序列化导致空白字段

时间:2014-05-21 15:11:43

标签: java rest jersey deserialization

我遇到了一个问题。当泽西客户通过电话

对我的POJO进行反序列化时
jersey.api.ClientResponse#getEntity(OrderCostSummary.class)

所有字段,即subTotal,shippingCost,tax和total在响应json中为空。

我最初认为使用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;
}

3 个答案:

答案 0 :(得分:1)

也许你试试这个:

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

请注意,我添加了'实现Serializable'这对于序列化/反序列化的POJO更好

答案 1 :(得分:0)

您的结构似乎并不总是相同的:

这个结构:

{
   "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"
         }
      }
   }
}

答案 2 :(得分:0)

答案是以下json应该通过调用

进行反序列化
jersey.api.ClientResponse#getEntity(ClassThatHoldsOrderCostSummary.class)

一旦对象被反序列化,我就可以调用

classThatHoldsOrderCostSummary.getOrderCostSummary()