Jackson - @JsonTypeInfo 属性被映射为 null? [英] Jackson - @JsonTypeInfo property is being mapped as null?

查看:37
本文介绍了Jackson - @JsonTypeInfo 属性被映射为 null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个回复:

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

然后,基于 exercise_type 属性,我想实例化不同的对象实例(ExerciseResponseDTO 的子类),所以我在:

Then, based on exercise_type attribute, I want to instantiate different objects instances (subclasses of ExerciseResponseDTO), so I create this mix in:

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

public abstract class ExerciseResponseDTO {

    private String id;
    private String statement;
    @JsonProperty(value = "exercise_type") private String exerciseType;

    // Getters and setters 
}

public class ExerciseQuestionResponseDTO
    extends ExerciseResponseDTO {}

public class ExerciseChoiceResponseDTO
    extends ExerciseResponseDTO {}

所以我创建了我的 ObjectMapper 如下

So I create my ObjectMapper as follows

ObjectMapper mapper = new ObjectMapper();
mapper.addMixIn(ExerciseResponseDTO.class, ExerciseMixIn.class);

我的测试:

ExerciseResponseDTO exercise = mapper.readValue(serviceResponse, ExerciseResponseDTO.class)
Assert.assertTrue(exercise.getClass() == ExerciseQuestionResponseDTO.class); // OK
Assert.assertEquals("decaa828741611e58bcffeff819cdc9f" exercise.getId()); // OK
Assert.assertEquals("question statement", exercise.getStatement()); // OK
Assert.assertEquals("QUESTION", exercise.getExerciseType()); // FAIL. Expected: "QUESTION", actual: null 

问题是,由于某种原因,用作 @JsonTypeInfo 上的属性的 exercise_type 属性被映射为 null.知道如何解决这个问题吗?

The problem is that, for some reason, the exercise_type attribute being used as property on @JsonTypeInfo is being mapped as null. Any idea how i can solve this?

推荐答案

最后,我在 API 文档

关于类型标识符可见性的注意事项:默认情况下,反序列化完全处理类型标识符的(在读取 JSON 期间使用)由 Jackson 编写,不会传递给反序列化器.然而,如果是这样需要,可以定义属性visible = true,其中case 属性将按原样传递给反序列化器(并通过设置setter 或字段)反序列化.

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.

所以解决方案只是简单地添加 'visible' 属性,如下所示

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.

这篇关于Jackson - @JsonTypeInfo 属性被映射为 null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆