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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何識(shí)別 Jackson 錯(cuò)誤中缺少的類型 ID?

如何識(shí)別 Jackson 錯(cuò)誤中缺少的類型 ID?

滄海一幻覺 2023-06-08 17:16:22
我正在使用 Jackson 將 JSON 寫入文本文件,JSON 表示從抽象類繼承的 2 個(gè)類,但無論是否使用兩個(gè)類或一個(gè)/或一個(gè)類,都會(huì)發(fā)生錯(cuò)誤。JSON 似乎編寫正確,但在閱讀時(shí),我收到以下錯(cuò)誤:Missing type id when trying to resolve subtype of [simple type, class model.BaseContact]: missing type id property 'type'     at [Source: (File); line: 52, column: 1]json as follows:    {   "allContacts" : [ {     "type" : "personal",    "addressCity" : "Hamilton",    "addressNum" : "199",   "addressPOBox" : null,    "addressPostCode" : null,    "addressStreet" : "River Rd",    "addressSuburb" : null,    "email" : null,    "latitude" : null,    "longitude" : null,    "name" : "silly simon",    "notes" : null,    "phoneNumber" : "09754321",    "photoBytes" : null,    "photoURL" : null  }, {    "type" : "personal",    "addressCity" : "Auckland",    "addressNum" : "482",    "addressPOBox" : null,    "addressPostCode" : null,    "addressStreet" : "Smith Rd",    "addressSuburb" : null,    "email" : null,    "latitude" : null,    "longitude" : null,    "name" : "paul smith",    "notes" : null,    "phoneNumber" : "0544555",    "photoBytes" : null,    "photoURL" : null  }, {    "type" : "personal",    "addressCity" : "Appleby",    "addressNum" : "123",    "addressPOBox" : null,    "addressPostCode" : null,    "addressStreet" : "Apple rd",    "addressSuburb" : null,    "email" : null,    "latitude" : null,    "name" : "Steve Jobbs",    "notes" : null,    "phoneNumber" : "08004343",    "photoBytes" : null,    "photoURL" : null  } ],  "size" : 3}錯(cuò)誤消息指的是第 52 行第 1 列,假設(shè)調(diào)試器從第 1 行開始,該行將是最后一個(gè)大括號(hào)之后的行。
查看完整描述

1 回答

?
收到一只叮咚

TA貢獻(xiàn)1821條經(jīng)驗(yàn) 獲得超5個(gè)贊

更新:

BusinessContactclass 應(yīng)該用@JsonTypeName("business")?和PersonContactclass with@JsonTypeName("personal")而不是注釋@JsonTypeName("type"),因?yàn)槟銘?yīng)該在繼承者中定義特定類型。

@JsonTypeInfo注釋完全可以從子類中刪除。

更新 2:

附加PersonContact類應(yīng)該有默認(rèn)構(gòu)造函數(shù):

public?PersonContact(){}

輸入 JSON 文件不是列表,它是具有兩個(gè)屬性的實(shí)體allContactssize。因此它不能映射到ArrayList<BaseContact>.?因此,應(yīng)創(chuàng)建具有這兩個(gè)屬性的新實(shí)體:

public class ContactsWrapper

{

? ?private List<BaseContact> allContacts;

? ?private int size;


? ?public List<BaseContact> getAllContacts()

? ?{

? ? ? return allContacts;

? ?}


? ?public void setAllContacts(List<BaseContact> allContacts)

? ?{

? ? ? this.allContacts = allContacts;

? ?}


? ?public int getSize()

? ?{

? ? ? return size;

? ?}


? ?public void setSize(int size)

? ?{

? ? ? this.size = size;

? ?}

}

應(yīng)該更改讀取 JSON 的代碼:


ContactsWrapper contactsWrapper = new ObjectMapper().readerFor(ContactsWrapper.class).readValue(new File(fn));

abl = contactsWrapper.getAllContacts();

現(xiàn)在 JSON 被映射到ContactsWrapper并使用 getter 將聯(lián)系人列表分配給abl變量。


查看完整回答
反對(duì) 回復(fù) 2023-06-08
  • 1 回答
  • 0 關(guān)注
  • 373 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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