杰克逊反序列化的多态类型 [英] Jackson deserialization of polymorphic types

查看:424
本文介绍了杰克逊反序列化的多态类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过一个杰克逊反序列化@JsonTypeInfo的示例,即:

I've seen an example of jackson deserialization @JsonTypeInfo, that is:

@JsonTypeInfo(
    use = JsonTypeInfo.Id.NAME,
    include = JsonTypeInfo.As.PROPERTY,
    property = "type")
@JsonSubTypes({
    @JsonSubTypes.Type(value = Cat.class, name = "cat"),
    @JsonSubTypes.Type(value = Dog.class, name = "dog")})
public class Animal {...}

我已经尝试过了,并且效果很好.现在,问题在于在示例类中从Animal引用了Cat和Dog,我想避免这种情况.有没有办法从Animal类中移走类型绑定并仍然有反序列化工作? 谢谢

I've tried it and it works fine. Now, the problem is that in the example classes Cat and Dog are referenced from Animal, which I want to avoid. Is there a way to move type binding from class Animal and still have deserialization work? Thanks

推荐答案

我在这里找到了答案: http://jira.codehaus.org/browse/JACKSON-654 .所以我可以使用:

I found an answer here: http://jira.codehaus.org/browse/JACKSON-654. So I could use:

mapper.registerSubtypes(Cat.class, Dog.class);

这篇关于杰克逊反序列化的多态类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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