Jackson JsonTypeInfo.As.EXTERNAL_PROPERTY无法正常工作 [英] Jackson JsonTypeInfo.As.EXTERNAL_PROPERTY doesn't work as expected

查看:815
本文介绍了Jackson JsonTypeInfo.As.EXTERNAL_PROPERTY无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Jackson来解析我无法控制的JSON。 JSON看起来像这样:

I am using Jackson to parse JSON that I have no control over. The JSON looks like this:

{
    "status":"0"
    "type":"type1"
    "info": {
       // additional fields
    }
}

我的课程看起来像这样

public class Response {
    private String status;
    private String type;
    private Info info
}

我使用的Info的子类依赖于type属性,所以我的信息映射是

The subclass of Info that I use depends on the type property, so my mapping for info is

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXTERNAL_PROPERTY, property = "type")
@JsonSubTypes(value = {
        @JsonSubTypes.Type(value = Type1Info.class, name = "type1"),
        @JsonSubTypes.Type(value = Type2Info.class, name = "type2") })
public abstract class Info {
    // some fields
}

据我所知,当区别元素与必须转换的元素处于同一级别时,这是使用类型信息的正确方法。但这不起作用,我总是得到同样的错误:

As far as I can tell this is the correct way to use type info when the distinguishing element is at the same level as the element that has to be casted. But this doesn't work, I always get the same error:


com.fasterxml.jackson.databind.JsonMappingException:意外的令牌
(END_OBJECT),预期FIELD_NAME:缺少属性'type',即
包含类型ID

com.fasterxml.jackson.databind.JsonMappingException: Unexpected token (END_OBJECT), expected FIELD_NAME: missing property 'type' that is to contain type id

如果我改变EXTERNAL_PROPERTY到PROPERTY我仍然得到相同的错误。我对EXTERNAL_PROPERTY的理解是错误的吗?

If I change EXTERNAL_PROPERTY to PROPERTY I still get the same error. Is my understanding of EXTERNAL_PROPERTY wrong?

推荐答案

来自Javadoc:


类似于PROPERTY的包含机制,除了属性是
在层次结构中包含一个更高级别,即作为与要键入的JSON对象相同级别的
的兄弟属性。请注意,此选项只能使用
用于属性
,而不能用于类型(类)。尝试将它用于
类将导致基本PROPERTY的包含策略。

Inclusion mechanism similar to PROPERTY, except that property is included one-level higher in hierarchy, i.e. as sibling property at same level as JSON Object to type. Note that this choice can only be used for properties, not for types (classes). Trying to use it for classes will result in inclusion strategy of basic PROPERTY instead.

注意到只能用于属性是粗体。资料来源: JsonTypeInfo .As.EXTERNAL_PROPERTY

Noticed that can only be used for properties is bolded. Source: JsonTypeInfo.As.EXTERNAL_PROPERTY.

因此,您必须将所有注释从 Info 类移到属性 info setInfo Response class中的方法。

So, you have to move all annotation from Info class to property info or setInfo method in Response class.

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXTERNAL_PROPERTY, property = "type")
@JsonSubTypes(value = { @JsonSubTypes.Type(value = Type1Info.class, name = "type1"),
        @JsonSubTypes.Type(value = Type2Info.class, name = "type2") })
public void setInfo(Info info) {
    this.info = info;
}

对我来说,你还应该删除 type 属性来自响应类。它将在序列化过程中动态生成。在反序列化中你不需要它,因为杰克逊关心类型。你的课程可能如下所示:

For me, you should also remove type property from Response class. It will be generated dynamically during serialization process. In deserialization you do not need it because Jackson cares about types. Your class could look like this:

class Response {

    private String status;
    private Info info;

    //getters, setters
}

另见问题: JSON嵌套类数据绑定

这篇关于Jackson JsonTypeInfo.As.EXTERNAL_PROPERTY无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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