如何在pojo上填充类型字段

如何在pojo上填充类型字段

本文介绍了Jackson 2.1多态反序列化:如何在pojo上填充类型字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从REST服务中提取了一个类别和项目树。类别具有子属性,其中包含类别和/或项目列表。它们的类型在kind字段中指定。

I am pulling a tree of Categories and Items from a REST service. Categories have a "child" attribute that contains a list of Categories and/or Items. Their types are specified in the field "kind".

杰克逊的多态类型处理非常好,所有工作都按预期工作,除了一个小故障:种类字段本身没有填充。有没有一种简单的方法可以将这些数据输入pojos?我希望不必编写自定义反序列化器。

Jackson's polymorphic type handling is great and all working as expected, except one small hitch: the "kind" field itself is not populated. Is there a simple way to get this data onto the pojos? I hope not to have to write custom deserializers.

这是Category和Item的基类。这两个子类添加了几个标量字段,并不是很有趣。

Here is the base class for Category and Item. The two subclasses add several scalar fields, and aren't very interesting.

@JsonIgnoreProperties(ignoreUnknown=true)
@JsonTypeInfo(
    use = JsonTypeInfo.Id.NAME,
    include = JsonTypeInfo.As.PROPERTY,
    property = "kind",
    defaultImpl = EntityBase.Impl.class
    )
@JsonSubTypes({
    @Type(value = Item.class, name = "Item"),
    @Type(value = Category.class, name = "Category")
    })
public abstract class EntityBase {
    String title;
    String kind;

    public void setTitle(String title) { this.title = title; }
    public String getTitle() { return title; }

    public void setKind(String kind) { this.kind = kind; }
    public String getKind() { return kind; }

    public static class Impl extends EntityBase {}
}

我正在使用类似这样的ObjectMapper进行反序列化:

I'm doing the deserialization with an ObjectMapper something like this:

ObjectMapper mapper = new ObjectMapper();
Category category = mapper.readValue(inputStream, Category.class);

我认为这是无关紧要的,它甚至不值得一个标签,但为了以防万一,这个是在一个Android应用程序。

I think it's so irrelevant that it doesn't even deserve a tag, but just in case, this is in an Android app.

推荐答案

像往常一样,我在发布此问题之前花了几分钟时间搜索,以确保我没有错过任何明显的事情。

As usual, I spent a few more minutes searching just before posting this question to make sure I hadn't missed anything obvious.

我不会说它显而易见,但我追查了和答案。该票据在上的帖子中以评论链接,尽管我已经丢失了帖子的链接。

I wouldn't call it obvious, but I tracked down a resolved jira ticket with the answer. The ticket was linked in comments under a post on http://jackson-users.ning.com/, though I've lost the link to the post.

JsonTypeInfo注释中有一个可见属性就是这样。

@JsonTypeInfo(
    use = JsonTypeInfo.Id.NAME,
    include = JsonTypeInfo.As.PROPERTY,
    property = "kind",
    visible = true,                    // <----- add this
    defaultImpl = EntityBase.Impl.class
    )
public abstract class EntityBase {
    ...
}

事实证明,这是在javadocs中记录的。由于旧的1.5文档上的优秀SEO(以及jackson.codehaus.org和fasterxml.com之间令人困惑的二分法没有帮助),我错过了它,但现在我已经吸取了教训,而且我正在查看文档这里:。

Turns out, this is documented in the javadocs. I had missed it thanks to the excellent SEO on the old 1.5 docs (and the confusing dichotomy between jackson.codehaus.org and fasterxml.com doesn't help), but now I've learned my lesson and I'm looking at docs here: http://wiki.fasterxml.com/JacksonJavaDocs.

这篇关于Jackson 2.1多态反序列化:如何在pojo上填充类型字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 13:38