Jackson error : no suitable constructor for a simple class

做~自己de王妃 提交于 2021-02-04 15:39:32

问题


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.

I have this exception as below:

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

}

And my unit test :

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());
    }
}
}

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

Any idea?

Thanks


回答1:


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.

Here is an example:

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));
    }
}



回答2:


You have no default (ie no-args) constructor.

Define a no-args constructor:

public Series() {}

The reason it works when you comment out the 3-arg constructor is in java if there are no constructors, the default constructor is implicitly defined.

This leads to the unexpected effect that if there aren't any constructors and you define a non-default constructor, the (implicit) default constructor disappears! Leading you, like many others before you, to wonder what is going on.



来源:https://stackoverflow.com/questions/25101627/jackson-error-no-suitable-constructor-for-a-simple-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!