L-Samuels
L-Samuels

Reputation: 2832

Jersey Jackson JSON deserialization of response entity results in blank fields

Ive come up against a problem. When my POJO is being deserialized by the Jersey client via the call

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

all the fields i.e subTotal, shippingCost, tax and total are null in the response json.

I initially thought it would have been a problem to do with the Money class, i.e the ObjectMapper not knowing how to deserialize this class but I have other api calls which return response entities with Money fields that deserialize without any issues.

This is the JSON being returned back from the server:

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

The 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;

My money 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;
}

Upvotes: 1

Views: 1922

Answers (3)

L-Samuels
L-Samuels

Reputation: 2832

The answer was that the following json should have been deserialized with the call

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

Once the object has been deserialized I can then call

classThatHoldsOrderCostSummary.getOrderCostSummary()

Upvotes: 0

Snorky35
Snorky35

Reputation: 425

Perhaps if you try this :

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

Notice that i added 'implements Serializable' which is better for POJO that are serialized/deserialized

Upvotes: 1

Basemasta
Basemasta

Reputation: 371

It seems that your structure is not always the same :

This structure :

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

is different from this structure :

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

Upvotes: 0

Related Questions