2016-09-27 3 views
3

각 코드 행을 살펴 보았지만 Jackson의 처리 방법을 생각합니다. 내부 다형성.Jackson의 @JsonTypeInfo (use = Id.CUSTOM, include = As.PROPERTY, property = "type")는 "type"을 제외한 JSON의 모든 필드를 읽습니다.

Animal을 확장 DogCat의 고전적인 예를 사용 :

@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입니다 그 exte nds AbstractTypeIdResolver. 매우 이상한 이유로

, bark_decibelmeow_level은 JSON에서 직렬화되어 있지만, typenull로지고 있습니다. 어떤 아이디어?

답변

2

설정 visible=true@JsonTypeInfo을 위해 :

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

는 참조 this post