2016-09-27 20 views
3

コードの各行を踏んでいますが、多形性を内部的に有する。Jacksonの@JsonTypeInfo(use = Id.CUSTOM、include = As.PROPERTY、property = "type")はJSONの "type"以外のすべてのフィールドを読み込みます

@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あるDogAnimalを拡張Catの典型的な例を用い

そのexte nds AbstractTypeIdResolver

いくつかの非常に奇妙な理由で、bark_decibelmeow_levelはJSONから直列化復元されているが、typenullとしてつつあります。何か案は? @JsonTypeInfoため

答えて

2

設定visible=true

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

this post

を参照してください。
関連する問題