【问题标题】:How to identify the missing type id in Jackson error?如何识别 Jackson 错误中缺少的类型 id?
【发布时间】:2019-08-17 09:23:03
【问题描述】:

我正在使用 Jackson 将 JSON 写入文本文件,JSON 表示从抽象类继承的 2 个类,但无论是否使用两者或其中一个/或类,都会发生错误。 JSON 似乎写入正确,但在阅读时,我收到以下错误:

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
}

错误消息指的是第 52 行第 1 列,假设调试器从第 1 行开始,这将是最后一个大括号之后的行。

BaseContact类头如下:

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;

@JsonTypeInfo(
        use=JsonTypeInfo.Id.NAME,
        include=JsonTypeInfo.As.PROPERTY,
        property="type")
@JsonSubTypes({
        @JsonSubTypes.Type(value=PersonContact.class, name= "personal"),
        @JsonSubTypes.Type(value= BusinessContact.class, name="business")
})

public  abstract class BaseContact {

public String name;
public String addressNum;
public String addressStreet;
public String addressSuburb;
public String addressCity;
public String addressPOBox;
public String addressPostCode;
public Double latitude;
public Double longitude;

public String photoURL;
public String photoBytes;
public String phoneNumber;
public String email;

public String notes;

public BaseContact() {
    //DEFAULT CONSTRUCTOR

}


    public BaseContact( String name, String addressNum, String addressStreet, 
    String addressCity, String phoneNumber) {

    this.name = name;
    this.addressNum = addressNum;
    this.addressStreet = addressStreet;
    this.addressCity = addressCity;
    this.phoneNumber = phoneNumber;
}

调用函数如下:

 public BusinessService readAllData(String fn) {
  ArrayList<BaseContact> abl = new ArrayList<BaseContact>();
            try {
                abl = new ObjectMapper().readerFor(BaseContact.class).readValue(new File(fn));
                Log.d("qq","abl"+ abl);
            } catch (IOException e) {
                Log.d("qq", "failed reading " + e.getMessage().toString());
                e.printStackTrace();
            }


             BusinessService b = new BusinessService();
            return b;
    }

BusinessContact类(继承自抽象BaseContact)如下:

package model;

import com.fasterxml.jackson.annotation.JsonTypeName;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
@JsonTypeInfo(
    use=JsonTypeInfo.Id.NAME,
    include=JsonTypeInfo.As.PROPERTY,
    property="type")
@JsonTypeName("type")
public class BusinessContact extends BaseContact {
public String companyName;
public String websiteURL;
public String businessHours;
//def constructor
public BusinessContact(){
};
public BusinessContact(String name,  String addressNum, String addressStreet, 
String addressCity, String phoneNumber, String companyName, String websiteURL, String businessHours) {
    super(name, addressNum, addressStreet, addressCity, phoneNumber);
    this.companyName = companyName;
    this.websiteURL = websiteURL;
    this.businessHours = businessHours;
}

//Getters and setters
public String getCompanyName() {
    return companyName;
}
public void setCompanyName(String companyName) {
    this.companyName = companyName;
}
public String getWebsiteURL() {
    return websiteURL;
}
public void setWebsiteURL(String websiteURL) {
    this.websiteURL = websiteURL;
}
public String getBusinessHours() {
    return businessHours;
}
public void setBusinessHours(String businessHours) {
    this.businessHours = businessHours;
}
public String visitWebsite(int i ){
    //get website, construct intent
    return"url intent";
}
public Boolean isOpen(int i ){
    //do math for day and time and return true if open
    return true;
}
@Override
public String toString() {
    String output= this.getClass() + "name: "+ this.name + " " + "company"+ this.companyName + "Hours "+ this.businessHours + "Website "+ this.websiteURL+ " address: " + this.addressNum+ " , " + this.addressStreet + " , " + this.addressSuburb+ "," +
            this.addressCity +" , CODE " + this.addressPostCode + " PO BOX " + this.addressPOBox + "PH: " +  this.phoneNumber + "email: " + this.email + "notes: "+ this.notes ;
    return output ;
}

}

PersonContact 类(继承自抽象 BaseContact):

package model;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeName;
@JsonTypeInfo(
        use = JsonTypeInfo.Id.NAME,
        include = JsonTypeInfo.As.PROPERTY,
        property = "type")
@JsonTypeName("type")
public class PersonContact extends BaseContact {
   //constructor
    public PersonContact(String name, String addressNum, String addressStreet, String addressCity, String phoneNumber) {
        super(name, addressNum, addressStreet, addressCity, phoneNumber);
    }
    @Override
    public String toString() {
        String output = this.getClass() + "name: " + this.name + " " + " address: " + this.addressNum + " , " + this.addressStreet + " , " + this.addressSuburb + "," +
                this.addressCity + " , CODE /n " + this.addressPostCode + " PO BOX " + this.addressPOBox + "PH: " + this.phoneNumber + "email: " + this.email + "notes: " + this.notes;
        return output;
    }
}

【问题讨论】:

  • 添加了继承自抽象 BaseContact 的 BusinessContact 和 PersonContact 类

标签: java json jackson deserialization json-deserialization


【解决方案1】:

更新:

BusinessContact 类应该用@JsonTypeName("business") 注释,PersonContact 类用@JsonTypeName("personal") 而不是@JsonTypeName("type"),因为你应该在继承者中定义特定的类型。

@JsonTypeInfo 注解完全可以从子类中移除。

更新 2:

另外PersonContact 类应该有默认构造函数:

public PersonContact(){}

输入 JSON 文件不是一个列表,它是一个具有两个属性 allContactssize 的实体。因此它不能映射到ArrayList&lt;BaseContact&gt;。所以应该创建一个具有这两个属性的新实体:

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

应该更改读取 JSON 的代码:

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

现在 JSON 被映射到 ContactsWrapper,并使用 getter 将联系人列表分配给 abl 变量。

【讨论】:

  • 谢谢,我已经进行了这些更改,但我仍然收到“在尝试解析 [简单类型,类 model.BaseContact] 的子类型时读取缺失类型 ID 失败:缺少类型 ID 属性“类型”“
  • 我已通过解决您的问题的其他步骤更新了答案。请参阅更新 2 部分。
  • 我不能赞成你的答案,但 UPDATE2 是有效的,非常感谢 Igor
  • NoArg 构造函数部分在这里很有帮助
猜你喜欢
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
  • 2021-02-19
  • 2017-02-15
  • 2017-12-11
  • 1970-01-01
  • 2020-05-23
  • 1970-01-01
相关资源
最近更新 更多