JSONmappingexception: No Suitable Constructor Found for Type [Simple Type, Class ]: Can Not Instantiate from JSON Object

JsonMappingException: No suitable constructor found for type [simple type, class ]: can not instantiate from JSON object

So, finally I realized what the problem is. It is not a Jackson configuration issue as I doubted.

Actually the problem was in ApplesDO Class:

public class ApplesDO {

private String apple;

public String getApple() {
return apple;
}

public void setApple(String apple) {
this.apple = apple;
}

public ApplesDO(CustomType custom) {
//constructor Code
}
}

There was a custom constructor defined for the class making it the default constructor. Introducing a dummy constructor has made the error to go away:

public class ApplesDO {

private String apple;

public String getApple() {
return apple;
}

public void setApple(String apple) {
this.apple = apple;
}

public ApplesDO(CustomType custom) {
//constructor Code
}

//Introducing the dummy constructor
public ApplesDO() {
}

}

com.fasterxml.jackson.databind.JsonMappingException: no suitable constructor found, can not deserialize from Object value

Add annotations @NoArgsConstructor and @AllArgsConstructor

objectmapper calls the default constructor to create the instance of the POJO first. Then each entry from the JSON is parsed and set in the instance using setters. Since you didn't have the default constructor, it failed with an appropriate exception. @NoArgsConstructor annotation provides the default constructor and it makes it work.

JsonMappingException: No suitable constructor found

I'm guessing your BlogPost class is an inner class of your activity. In that case Java adds a hidden field to each object that refers to the containing object. And this implicit field is of course not present in your JSON data.

To solve this, you should either keep the BlogPost class in a separate file (so that it's not an inner class) or mark it as a static class (which removes the implicit field):

public static class BlogPost {

JsonMappingException: No suitable constructor found for type [simple type, class car.Car$Parts]

The answer

The Part class is a nested, non-static class.
In order to instantiate it, you must have an instance of the outer class.

Here is an inner class tutorial

I-hate-reading answer

Move the Part and Parts classes to their own java files, named Part.java and Parts.java.

Jersey: No suitable constructor found for type [simple type, class Thing]: can not instantiate from JSON object

You need a no-arg constructor and setters, or use @JsonCreator. Easiest thing to do would be just to add the no-arg with setters. Jackson needs the setters when deserializing. For serialization, all that's needed are getters.

EDIT

To keep it immutable, you can use @JsonCreator on the constructor. For example

@JsonCreator
public Thing(@JsonProperty("symbol") String symbol,
@JsonProperty("name") String name) {

this.symbol = symbol;
this.name = name;
}

See more Jackson Annotations: @JsonCreator demystified

Jackson error : no suitable constructor for a simple class

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


Related Topics



Leave a reply



Submit