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

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

问题描述

我正在处理这个问题。假设我有这样的回复:

I am dealing with this problem. Let's say i have this response:

{  
    "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 如下

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 

问题在于,出于某种原因, exercise_type 属性被用作 @JsonTypeInfo 的属性被映射为 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 Doc


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

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.

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

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