[Solved] org.codehaus.jackson.map.JsonMappingException: 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 this exception.

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

Solution:

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 !!

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

3 thoughts on “[Solved] org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type”

  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

HowToDoInJava

A blog about Java and related technologies, the best practices, algorithms, and interview questions.