7

我逐行浏览了每一行代码,但我认为这是 Jackson 在内部处理多态性的方式。

使用DogCat扩展的经典示例Animal

@JsonTypeInfo(use = Id.CUSTOM, include = As.PROPERTY, property = "type")
@JsonTypeIdResolver(AnimalTypeIdResolver.class)
@JsonIgnoreProperties(ignoreUnknown = true)
public abstract class Animal implements Serializable {
    public AnnotatorBundleConfig(String name) {
        super();
        this.name = name;
    }

狗类:

public class DogAnimal extends Animal {
    @JsonCreator
    public DogAnimal(
        @JsonProperty(value="name", required=true) String name,
        @JsonProperty(value="bark_decibel") int bark_decibel)
    {
    super(name);
    this.bark_decibel = bark_decibel;}

猫类:

public class CatAnimal extends Animal {
    @JsonCreator
    public CatAnimal(
        @JsonProperty(value="name", required=true) String name,
        @JsonProperty(value="meow_level") int meow_level)
    {
    super(name);
    this.meow_level = meow_level;}

AnimalTypeIdResolver是一个典型的 TypeIdResolver 扩展AbstractTypeIdResolver

出于一些非常奇怪的原因,bark_decibel并且meow_level从 JSON 反序列化,但typenull. 有任何想法吗?

4

1 回答 1

6

设置visible=true@JsonTypeInfo

@JsonTypeInfo(use = Id.CUSTOM, include = As.PROPERTY, property = "type", visible=true)

参考这篇文章

于 2016-09-27T02:08:25.843 回答