Search code examples
javajsonjacksonobjectmapper

Json to Java Object - Failed due to identical fields


I am trying to convert a json to a java object. Since there are identical fields in the json, it throws an error like this.

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)

This is the json ( part related to my problem)

   "order":{  
      "amount":1.000,
      "chargeback":{  
         "amount":0,
         "currency":"BHD"
      },
   }

This is the relevant part of java class.

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

Please note that Chargeback class is in the Order.java file.

I have tried @JsonIgnore annotation and removing the amount in the chargeback class but still the error exists. Could someone please suggest a solution for this?


Solution

  • I have modified your code, Try the code below. Basically, I have done the followings.

    1. There was no getter and setter for Chargeback object inside Order, so it was addedd.
    2. Commented out the @JsonIgnore annotations.

    The classess are given below

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

    Code to Test to generate Json is given below.

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

    Generate json is given below.

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