【发布时间】:2019-06-06 14:38:34
【问题描述】:
我正在尝试将 json 转换为 java 对象。由于json中有相同的字段,所以会抛出这样的错误。
com.fasterxml.jackson.databind.JsonMappingException: Conflicting setter definitions for property "amount":
com.gateway.model.Order#setAmount(1 params) vs com.gateway.model.Order#setAmount(1 params)
这是json(与我的问题有关的部分)
"order":{
"amount":1.000,
"chargeback":{
"amount":0,
"currency":"BHD"
},
}
这是java类的相关部分。
public class Order
{
private double amount;
Chargeback ChargebackObject;
// Getter Methods
public double getAmount()
{
return amount;
// Setter Methods
public void setAmount(double amount)
{
this.amount = amount;
}
}
class Chargeback
{
private double amount;
private String currency;
// Getter Methods
@JsonIgnore
public double getAmount()
{
return amount;
}
@JsonInclude(Include.NON_NULL)
public String getCurrency()
{
return currency;
}
// Setter Methods
public void setAmount(double cb_amount)
{
this.amount = cb_amount;
}
public void setCurrency(String currency)
{
this.currency = currency;
}
}
请注意 Chargeback 类在 Order.java 文件中。
我尝试了@JsonIgnore 注释并删除了chargeback 类中的amount,但错误仍然存在。有人可以为此提出解决方案吗?
【问题讨论】:
-
这两个类之间似乎有两个同名的方法(
setAmount);您需要重命名其中一个属性或向 Jackson 指明是哪个属性。尝试将您的订单金额重命名为“数量”并将 setAmount 重命名为 setQuantity,因为它可以更好地表达您想要完成的任务。 -
@AustinSchäfer 正是如此。嗯,这是一个有很多变量和方法的大类,不知何故我错过了它。感谢您指出。 :)
标签: java json jackson objectmapper