gpt4 book ai didi

java - Jackson 具有抽象类的多态行为

转载 作者:行者123 更新时间:2023-11-30 07:33:47 28 4
gpt4 key购买 nike

有一个包含各种字段的类DocumentMapping。这些字段之一是抽象类 BinaryDocumentMappingBinaryDocumentMapping 由其他 3 个类 ExcelDocumentMappingXMLDocumentMappingCSVDocumentMapping 扩展。

所以我有:

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;

哪里

public abstract class BinaryDocumentMapping
implements Serializable {

@JsonTypeName("CSV")
public class CsvDocumentMapping

序列化工作正常,但在将 json 读入对象时遇到问题:

mapper.reader().withType(DocumentMapping.class).readValue(jsonData);

抛出

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

有没有办法根据类型反序列化为具体类?

最佳答案

关于java - Jackson 具有抽象类的多态行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35700170/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com