简体   繁体   English

杰克逊与抽象类的多态行为

[英]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个类ExcelDocumentMappingXMLDocumentMappingCSVDocumentMapping

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 of com.documentmapping.BinaryDocumentMapping , problem: abstract types either need to be mapped to concrete types, have custom deserializer, or be instantiated with additional type information com.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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM