[英]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
当我的POJO被Jersey客户通过电话反序列化时
jersey.api.ClientResponse#getEntity(OrderCostSummary.class)
all the fields ie subTotal, shippingCost, tax and total are null in the response json. 响应json中的所有字段,即subTotal,shippingCost,tax和total均为空。
I initially thought it would have been a problem to do with the Money class, ie 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. 最初,我认为与Money类有关将是一个问题,即ObjectMapper不知道如何反序列化此类,但是我有其他api调用,它们返回带有Money字段的响应实体,这些响应字段反序列化而没有任何问题。
This is the JSON being returned back from the server: 这是从服务器返回的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"
}}}
The POJO 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 我的钱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;
}
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 请注意,我添加了“实现序列化”,这对于经序列化/反序列化的POJO更好
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"
}
}
}
}
The answer was that the following json should have been deserialized with the call 答案是以下json应该已经通过调用反序列化了
jersey.api.ClientResponse#getEntity(ClassThatHoldsOrderCostSummary.class)
Once the object has been deserialized I can then call 对象反序列化后,我可以调用
classThatHoldsOrderCostSummary.getOrderCostSummary()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.