Jackson 3.1.1缺少多态列表类型的子类型异常的类型ID?

时间:2019-08-05 21:16:35

标签: java json jackson jackson-databind

我有一个抽象类的具体对象。

我在抽象类和子类上使用批注,但是JSON输出看起来不正确,并且在反序列化过程中不断出现异常。

我仍在学习杰克逊(Jackson),不幸的是,许多关于该主题的教程已经过时了。

以下是带有我的对象映射器的类:

抽象类:

@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, property = "BTRFSPhysicalLocationItem")
@JsonSubTypes({
    @Type(name = "Snapshot", value = Snapshot.class),
    @Type(name = "Backup", value = Backup.class),
    @Type(name = "Subvolume", value = Subvolume.class)})
public abstract class BTRFSPhysicalLocationItem {

    private final String name;
    @JsonProperty
    private final Path location;

    /**
     * Makes a new subvolume instance with the specified location. May or may
     * not actually exist.
     *
     * @param location The location of the subvolume
     */
    @JsonCreator
    protected BTRFSPhysicalLocationItem(@JsonProperty(value = "location") Path location) {
        this.location = location;
        this.name = location.getFileName().toString();
    }

具体类别:

public class Subvolume extends BTRFSPhysicalLocationItem {

    /**
     * Creates a new subvolume with the specified path.The subvolume may or may
     * not exist at this point.
     *
     * @param location
     */
    @JsonCreator
    public Subvolume(@JsonProperty Path location) {
        super(location);
    }

Objectmapper块:

ObjectMapper MAPPER = new ObjectMapper();
List<Subvolume> SUBVOLUME_LIST = new ArrayList<>();
//Subvolume is populated by other methods it's not worth showing them here
System.out.println("\n\n");
                MAPPER.writeValue(System.out, SUBVOLUME_LIST);
                System.out.println("\n\n\n");
                var s = MAPPER.writeValueAsString(SUBVOLUME_LIST);
                List<Subvolume> list = MAPPER.readValue(s, new TypeReference<List<Subvolume>>() {
                });

输出JSON: [{"name":"WanderingEcho","location":"file:///home/sarah/NetBeansProjects/WanderingEcho/"}]

例外:

Aug 05, 2019 4:55:14 PM com.protonmail.sarahszabo.wanderingecho.btrfs.BTRFS <clinit>
SEVERE: null
com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Missing type id when trying to resolve subtype of [simple type, class com.protonmail.sarahszabo.wanderingecho.btrfs.subvolume.Subvolume]: missing type id property 'BTRFSPhysicalLocationItem'
 at [Source: (String)"[{"name":"WanderingEcho","location":"file:///home/sarah/NetBeansProjects/WanderingEcho/"}]"; line: 1, column: 89] (through reference chain: java.util.ArrayList[0])

诸如Jackson deserialization of polymorphic types之类的其他答案建议使用ObjectMapper方法,但是根据我阅读的教程,这不是必需的。

不知道我在注释中做错了什么。

1 个答案:

答案 0 :(得分:1)

在您的json中,您缺少BTRFSPhysicalLocationItem-想一想-Jackson应该如何知道您期望实现什么?

应该看起来像

[
    {
        "BTRFSPhysicalLocationItem": "Subvolume",
        "name": "WanderingEcho",
        "location":"file:///home/sarah/NetBeansProjects/WanderingEcho/"
    }
]

如果您希望Jackson在序列化对象时包括类型,则还需要添加JsonTypeInfo.As.PROPERTY作为include作为@JsonTypeInfo批注的参数,例如

@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "BTRFSPhysicalLocationItem")

编辑,好吧,我已经弄清楚了-您未生成属性的问题与Java类型擦除有关,并得到了here的详尽描述。

tldr;杰克逊在理解类型方面有问题-似乎List<Subvolume>因为他没有提供元素的类型。因为他不知道类型,所以不会序列化

解决方案是创建一个将扩展ArrayList<Subvolume>的辅助类,

public class SubvolumeList extends ArrayList<Subvolume> {}

此外,您的代码中还有一些问题-一个问题是,如果要按属性值(例如use = JsonTypeInfo.Id.NAME)而不是按package /进行反序列化,则应在@JsonTypeInfo中使用Subvolume类字符串文字

第二,您应该在@JsonProperty参数列表中将@JsonCreator命名为@JsonProperty(value = "location")