[英]Jackson polymorphic behaviour with abstract class
Having a class DocumentMapping
that contains various fields. 有一个包含各种字段的DocumentMapping
类。 One of those fields is a class BinaryDocumentMapping
which is abstract. 这些字段BinaryDocumentMapping
是抽象的BinaryDocumentMapping
类。 BinaryDocumentMapping
is extended by 3 other classes ExcelDocumentMapping
, XMLDocumentMapping
and CSVDocumentMapping
. BinaryDocumentMapping
由其他3个类ExcelDocumentMapping
, XMLDocumentMapping
和CSVDocumentMapping
。
So I'm having : 所以我有:
public class DocumentMapping{
@JsonSubTypes({ @JsonSubTypes.Type(value = ExcelDocumentMapping.class, name = "EXCEL"),
@JsonSubTypes.Type(value = CsvDocumentMapping.class, name = "CSV"), @JsonSubTypes.Type(value = XmlDocumentMapping.class, name = "XML") })
private BinaryDocumentMapping binary = null;
where 哪里
public abstract class BinaryDocumentMapping
implements Serializable {
and 和
@JsonTypeName("CSV")
public class CsvDocumentMapping
The serialization works ok but I have problem when reading the json into an object: 序列化工作正常,但是将json读入对象时出现问题:
mapper.reader().withType(DocumentMapping.class).readValue(jsonData);
which throws 哪个抛出
com.fasterxml.jackson.databind.JsonMappingException
: Can not construct instance ofcom.documentmapping.BinaryDocumentMapping
, problem: abstract types either need to be mapped to concrete types, have custom deserializer, or be instantiated with additional type informationcom.fasterxml.jackson.databind.JsonMappingException
:无法构造com.documentmapping.BinaryDocumentMapping
实例,问题:抽象类型要么需要映射到具体类型,要么具有自定义反序列化器,或者使用其他类型信息实例化
Is there any way to deserialize into the concrete class based on the type ? 有什么方法可以根据类型反序列化为具体类?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.