Json转Java对象-由于字段相同而失败

时间:2019-06-06 14:38:34

标签: java json jackson objectmapper

我正在尝试将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注释并删除了amount类中的chargeback,但是仍然存在错误。有人可以提出解决方案吗?

1 个答案:

答案 0 :(得分:1)

我已修改您的代码,请尝试以下代码。基本上,我已经完成了以下工作。

  1. Order内没有Chargeback对象的getter和setter,因此已添加。
  2. 注释了@JsonIgnore注释。

以下是课程

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

    public class Order {
      private double amount;
      Chargeback ChargebackObject;

      public double getAmount() {
        return amount;
      }

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

      public Chargeback getChargebackObject() {
        return ChargebackObject;
      }

      public void setChargebackObject(Chargeback chargebackObject) {
        ChargebackObject = chargebackObject;
      }
    }

生成Json的测试代码如下。

public class Test1 {
  public static void main(String[] args) throws Exception {

    Chargeback chargeback = new Chargeback();
    chargeback.setAmount(1234.00);
    chargeback.setCurrency("BHD");
    Order order = new Order();
    order.setAmount(2345.00);
    order.setChargebackObject(chargeback);

    ObjectMapper mapper = new ObjectMapper();
    String toJson = null;
    try {
      toJson = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(order);
    } catch (IOException e) {
      e.printStackTrace();
    }
    System.out.println("Complete Json = " + toJson);

// From Json String to Java Object
ObjectMapper mapper1 = new ObjectMapper();
Order order1 = mapper.readValue(toJson, Order.class);
System.out.println("Order Object -> " + order1);
  }
}

生成json如下。

{
  "amount" : 2345.0,
  "chargebackObject" : {
    "amount" : 1234.0,
    "currency" : "BHD"
  }
}