Jackson - @JsonTypeInfo property is being mapped as null?

前端 未结 2 705
情深已故
情深已故 2020-12-25 09:32

I have this response:

{  
    \"id\":\"decaa828741611e58bcffeff819cdc9f\",
    \"statement\":\"question statement\",
    \"exercise_type\":\"QUESTION\"
}
         


        
2条回答
  •  误落风尘
    2020-12-25 10:33

    Finally, I've found the solution in the API Doc

    Note on visibility of type identifier: by default, deserialization (use during reading of JSON) of type identifier is completely handled by Jackson, and is not passed to deserializers. However, if so desired, it is possible to define property visible = true in which case property will be passed as-is to deserializers (and set via setter or field) on deserialization.

    So the solution was simply adding the 'visible' attribute as follows

    @JsonTypeInfo(  
        use = JsonTypeInfo.Id.NAME,  
        include = JsonTypeInfo.As.PROPERTY,  
        property = "exercise_type",
        visible = true)  
    @JsonSubTypes({  
        @Type(value = ExerciseChoiceResponseDTO.class, name = "CHOICE"),  
        @Type(value = ExerciseQuestionResponseDTO.class, name = "QUESTION")})  
    public abstract class ExerciseMixIn  
    {}  
    

    Hope this helps someone else.

提交回复
热议问题