Java 失踪财产';类型';而映射的反序列化包含混合子类型-多态性

Java 失踪财产';类型';而映射的反序列化包含混合子类型-多态性,java,json,jackson,hashmap,polymorphism,Java,Json,Jackson,Hashmap,Polymorphism,我有一个名为Animal的对象,它有两个子类,Monkey和Lion,我使用JsonTypeInfo来支持子类型检测,我认为序列化JsonTypeInfo时,应该根据类自动放置名为“type”的属性 动物类 import java.io.Serializable; import com.fasterxml.jackson.annotation.JsonSubTypes; import com.fasterxml.jackson.annotation.JsonTypeInfo; @JsonTyp

我有一个名为Animal的对象,它有两个子类,Monkey和Lion,我使用JsonTypeInfo来支持子类型检测,我认为序列化JsonTypeInfo时,应该根据类自动放置名为“type”的属性

动物类

import java.io.Serializable;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonSubTypes({
        @JsonSubTypes.Type(value = Lion.class, name = "Lion"),
        @JsonSubTypes.Type(value = Monkey.class, name = "Monkey")
})
public class Animal implements Serializable {
    
    String name;

    public Animal(String name) {
        super();
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Animal other = (Animal) obj;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }

}

狮子班


public class Lion extends Animal{

    public Lion(String name) {
        super(name);
    }

}


猴子班


public class Monkey extends Animal{

    public Monkey(String name) {
        super(name);
    }

}

主要功能

    public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException {
        Map<String,Animal> animals = new HashMap<String, Animal>();
        animals.put("Bob", new Lion("Bob"));
        animals.put("Alice", new Monkey("Alice"));
        StringWriter sw = new StringWriter();
        ObjectMapper mapper = new ObjectMapper();
        mapper.setSerializationInclusion(Include.NON_NULL);
        mapper.writeValue(sw, animals);
        String animlasStr = sw.toString();
        Map<String,Animal> dAnimalMap = mapper.readValue(animlasStr, new TypeReference<Map<String, Animal>>() {});
        assert dAnimalMap.get("Alice") instanceof Monkey;
    }
我尝试了类型引用和visible属性,得到了相同的结果

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type" , visible =true)
而不是:
mapper.writeValue(西南,动物)
使用
mapper.writerFor(newtypereference(){}).writeValue(sw,动物)

而不是:
mapper.readValue(animlastr,newtypereference(){})
使用:
mapper.readValue(animlastr,newtypereference(){})

所有类中都有空构造函数

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type" , visible =true)