亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Json 到 Java 對象 - 由于字段相同而失敗

Json 到 Java 對象 - 由于字段相同而失敗

紅顏莎娜 2023-01-05 16:50:16
我正在嘗試將 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 回答

?
瀟瀟雨雨

TA貢獻1833條經驗 獲得超4個贊

我修改了你的代碼,試試下面的代碼?;旧希易隽艘韵聨准?。

  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"

  }

}


查看完整回答
反對 回復 2023-01-05
  • 1 回答
  • 0 關注
  • 315 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號