[Solved] Jackson: No suitable constructor found for type

If you are working on Jackson library and trying to marshal/unmarshal Java objects to/from JSON documents, you may encounter JsonMappingException error. 1. JsonMappingException When we are parsing a JSON file into Java object or we are converting the Java object to JSON representation, we must have created a …

If you are working on Jackson library and trying to marshal/unmarshal Java objects to/from JSON documents, you may encounter JsonMappingException error.

1. JsonMappingException

When we are parsing a JSON file into Java object or we are converting the Java object to JSON representation, we must have created a POJO class. During marshalling or unmarshalling we may face this issue.

The exception stack-trace for JsonMappingException looks like this:

org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type [simple type, class test.jackson.Employee]: can not instantiate from JSON object (need to add/enable type information?)
 at [Source: c:\temp\employee.json; line: 1, column: 2]
	at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163)
	at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:483)
	at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:350)
	at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2395)
	at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1549)
	at test.jackson.JSONToJavaExample.main(JSONToJavaExample.java:19)
exceptions-notes

2. Solution

In runtime, Jackson uses the default constructor to create the instance of POJO classes.

To solve this exception, all you have to do is to create the default constructor for your POJO class which you are trying to marshal/unmarshal. This simple solution will fix this error.

You are missing default constructor in your POJO class. Provide one.

Happy Learning !!

Leave a Comment

  1. I covert list to json and json back to list

    
    ObjectMapper objectMapper = new ObjectMapper();
    
                // Convert list to json
                String json = objectMapper.writeValueAsString(personList);
    
                System.out.println(json);
    
                // Convert json back to list
                List<Person> person = objectMapper.readValue(json, new TypeReference<List<Person>>(){});
    
    

    Is resullt:

    run:
    [{“firstName”:”Mike”,”lastName”:”harvey1″,”age”:34,”contact”:”001894536″},{“firstName”:”Mike”,”lastName”:”harvey2″,”age”:34,”contact”:”001894536″},{“firstName”:”Mike”,”lastName”:”harvey3″,”age”:34,”contact”:”001894536″},{“firstName”:”Mike”,”lastName”:”harvey4″,”age”:34,”contact”:”001894536″}]
    org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type [simple type, class gson.Main$Person]: can not instantiate from JSON object (need to add/enable type information?)
    at [Source: java.io.StringReader@4566e5bd; line: 1, column: 3]
    at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163)
    at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObjectUsingNonDefault(BeanDeserializer.java:740)
    at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:683)
    at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:580)
    at org.codehaus.jackson.map.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:217)
    at org.codehaus.jackson.map.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:194)
    at org.codehaus.jackson.map.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:30)
    at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2723)
    at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1861)
    at gson.Main.main(Main.java:48)
    BUILD SUCCESSFUL (total time: 0 seconds)

    Reply
  2. It solve the problem. But I don’t want to use default Constructor. As I am using Jackson annotation (@JsonDeserialize) to deserialize and serialize.

    Below, I am writing the sample code:

    @JsonInclude(Include.NON_NULL)
    @JsonDeserialize(builder = Employee.Builder.class)
    public final class Employee {
    private final String name;
    public Employee(final String name) {
    this.name = name;
    }
    public String getName() {
    return name;
    }
    public static final class Builder {
    private final String name;
    public Builder() {
    this.name = “”;
    }

    public Builder setName(
    final String name) {
    this.name = name;
    return this;
    }
    public Employee build() {
    return new Employee (name);
    }
    }
    }

    WebSerive:
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public class Resource {
    @POST
    @Path(PathConstants.EmployeePath)
    public Response postEmployee(final Employee employee) {
    //some operation.
    }

    Error: Getting the same error as Lokesh mentioned above.

    Thanks

    Reply

Leave a Comment

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.