如何使用JsonCreator对具有重载构造函数的类进行反序列化 [英] How to deserialize a class with overloaded constructors using JsonCreator

查看:730
本文介绍了如何使用JsonCreator对具有重载构造函数的类进行反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Jackson 1.9.10反序列化此类的实例:

I am trying to deserialize an instance of this class using Jackson 1.9.10:

public class Person {

@JsonCreator
public Person(@JsonProperty("name") String name,
        @JsonProperty("age") int age) {
    // ... person with both name and age
}

@JsonCreator
public Person(@JsonProperty("name") String name) {
    // ... person with just a name
}
}

当我尝试这个时,我得到以下内容

When I try this I get the following


冲突的基于属性的创建者:已经有... {interface org.codehaus.jackson.annotate.JsonCreator @ org.codehaus.jackson.annotate .JsonCreator()}],遇到......,注释:{interface org.codehaus.jackson.annotate.JsonCreator @ org.codehaus.jackson.annotate.JsonCreator()}]

Conflicting property-based creators: already had ... {interface org.codehaus.jackson.annotate.JsonCreator @org.codehaus.jackson.annotate.JsonCreator()}], encountered ... , annotations: {interface org.codehaus.jackson.annotate.JsonCreator @org.codehaus.jackson.annotate.JsonCreator()}]

有没有办法使用Jackson对带有重载构造函数的类进行反序列化?

Is there a way to deserialize a class with overloaded constructors using Jackson?

谢谢

解决方案

虽然没有正确记录,但每种类型只能有一个创建者。你可以在你的类型中拥有任意数量的构造函数,但只有其中一个构造函数应该有一个 @JsonCreator 注释。

Though its not properly documented, you can only have one creator per type. You can have as many constructors as you want in your type, but only one of them should have a @JsonCreator annotation on it.

这篇关于如何使用JsonCreator对具有重载构造函数的类进行反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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