杰克逊错误:没有适合简单类的构造函数 [英] Jackson error : no suitable constructor for a simple class

查看:51
本文介绍了杰克逊错误:没有适合简单类的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很麻烦,这是我想用Jackson 2.3.2进行序列化/反序列化的类. 序列化工作正常,但反序列化效果不好.

I am in trouble, here is a class I want to Serialize/Deserialize with Jackson 2.3.2. The serialization works fine but not the deserialization.

我有以下例外情况:

找不到适合类型[简单类型,类Series]的构造函数:无法从JSON对象实例化(需要添加/启用类型信息吗?)

No suitable constructor found for type [simple type, class Series]: can not instantiate from JSON object (need to add/enable type information?)

最奇怪的是,如果我评论构造函数,它会完美地工作!

The weirdest thing is that it works perfectly if I comment the constructor!

public class Series {

private int init;
private String key;
private String color;

public Series(String key, String color, int init) {
    this.key = key;
    this.init = init;
    this.color = color;
}

//Getters-Setters

}

我的单元测试:

public class SeriesMapperTest {

private String json = "{\"init\":1,\"key\":\"min\",\"color\":\"767\"}";
private ObjectMapper mapper = new ObjectMapper();

@Test
public void deserialize() {
    try {
        Series series = mapper.readValue(json, Series.class);
    } catch (IOException e) {
        Assert.fail(e.getMessage());
    }
}
}

此异常是从Jackson lib BeanDeserializerBase的方法deserializeFromObjectUsingNonDefault()抛出的.

This exception is throwing from the method deserializeFromObjectUsingNonDefault() of BeanDeserializerBase of Jackson lib.

有什么主意吗?

谢谢

推荐答案

Jackson并不要求类具有默认构造函数.您可以使用 @JsonCreator注释注释退出的构造函数并进行绑定使用@JsonProperty批注将构造函数的参数传递给属性. 注意:如果只有一个构造函数,甚至可以取消@JsonCreator.

Jackson does not impose the requirement for classes to have a default constructor. You can annotate the exiting constructor with the @JsonCreator annotation and bind the constructor parameters to the properties using the @JsonProperty annotation. Note: @JsonCreator can be even suppressed if you have single constructor.

这种方法的优点是可以创建真正的不可变对象,这对于各种

This approach has an advantage of creating truly immutable objects which is a good thing for various good reasons.

这里是一个例子:

public class JacksonImmutable {
    public static class Series {

        private final int init;
        private final String key;
        private final String color;

        public Series(@JsonProperty("key") String key,
                      @JsonProperty("color") String color,
                      @JsonProperty("init") int init) {
            this.key = key;
            this.init = init;
            this.color = color;
        }

        @Override
        public String toString() {
            return "Series{" +
                    "init=" + init +
                    ", key='" + key + '\'' +
                    ", color='" + color + '\'' +
                    '}';
        }
    }

    public static void main(String[] args) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        String json = "{\"init\":1,\"key\":\"min\",\"color\":\"767\"}";
        System.out.println(mapper.readValue(json, Series.class));
    }
}

这篇关于杰克逊错误:没有适合简单类的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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