第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

使用 jackson 轉(zhuǎn)換時 POJO 具有空值

使用 jackson 轉(zhuǎn)換時 POJO 具有空值

湖上湖 2022-11-30 11:24:54
我試圖尋找這個問題的答案,但意識到有多個相似但沒有一個與這個匹配。我有一個具有這種結(jié)構(gòu)的 JSON 對象{  "model": {     "serie" : "123456",      "id" : "abc123"    /// many fields  },  "externalModel": {    "serie" : "123456",    "fieldX" : "abcde"   // many fields as well}我在我的代碼中這樣做: ObjectMapper mapper = new ObjectMapper(); MyObject object = mapper.readValue(hit.getSourceAsString(), MyObject.class);其中 MyObject 具有以下形式:@JsonInclude(value = JsonInclude.Include.NON_NULL)@JsonIgnoreProperties(ignoreUnknown = true)public class MyObject {    @JsonProperty("serie")    String serie;    @JsonProperty("id")    Long id;    MyObject() {}   }當(dāng)我轉(zhuǎn)換時,我沒有得到任何異常,而是我得到了所有值都設(shè)置為null的 myObject我不知道會出什么問題,因為沒有返回任何異常,知道嗎?
查看完整描述

2 回答

?
皈依舞

TA貢獻(xiàn)1851條經(jīng)驗 獲得超3個贊

您需要使用根屬性model,


您可以重命名MyObject并MyModel創(chuàng)建一個MyObject


@JsonInclude(value = JsonInclude.Include.NON_NULL)

@JsonIgnoreProperties(ignoreUnknown = true)

public class MyObject{

    @JsonProperty("model")

    MyModel model;

}

然后檢查model


查看完整回答
反對 回復(fù) 2022-11-30
?
GCT1015

TA貢獻(xiàn)1827條經(jīng)驗 獲得超4個贊

實際上,您需要 MyObject 中的兩個對象。


@JsonInclude(value = JsonInclude.Include.NON_NULL)

@JsonIgnoreProperties(ignoreUnknown = true)

public class MyModel {


    @JsonProperty("id")

    private String id;


    @JsonProperty("serie")

    private String serie;


   //Generate getters and setters of these two


}


@JsonInclude(value = JsonInclude.Include.NON_NULL)

@JsonIgnoreProperties(ignoreUnknown = true)

public class ExternalObject {


    @JsonProperty("serie")

    private String serie;


    @JsonProperty("fieldX")

    private String fieldX;


   //Generate getters and setters of these two

}


@JsonInclude(value = JsonInclude.Include.NON_NULL)

@JsonIgnoreProperties(ignoreUnknown = true)

public class MyObject{


    @JsonProperty("model")

    private MyModel model;


    @JsonProperty("externalModel")

    private ExternalObject externalModel;


   //Generate getters and setters of these two  

}

現(xiàn)在,當(dāng)您像下面那樣使用它時,它會正常工作。


ObjectMapper mapper = new ObjectMapper();

MyObject object = mapper.readValue(hit.getSourceAsString(), MyObject.class);


查看完整回答
反對 回復(fù) 2022-11-30
  • 2 回答
  • 0 關(guān)注
  • 125 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號