Home  >  Forum  >  Core Java
Post New Query

Caused by: com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type: can not instantiate from JSON object


joined on
August 11,2014
Asked on April 23,2016
I am converting a bean into JSON and getting the error.
Why I am getting below error?

Caused by: com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type: can not instantiate from JSON object (missing default constructor or creator, or 

perhaps need to add/enable type information?)
 at [Source: java.io.PushbackInputStream@3edd9783; line: 1, column: 2]
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1106)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:296)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:133)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3736)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2810)
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:222)
... 40 common frames omitted
joined on
November 18,2013
Replied on April 23,2016

You need to create a default constructor in your bean. You have to create default constructor only if there is a constructor with one or more than arguments. Suppose your bean is as follows.

public class MyBean {
//getters
//setters
}

Then no need to create default constructor. But if your class has any constructor with arguments as below.
public class MyBean {
  private int age;
  public MyBean(int age) {
      this.age = age;
  }
//getters
//setters
}

Then we must have to create default constructor which is used by Jackson to instantiate bean using reflection.
public class MyBean {
  private int age;
  public MyBean() {}  //default constructor
  public MyBean(int age) {
      this.age = age;
  }
//getters
//setters
}

Write Answer




Copyright ©2017 concretepage.com, all rights reserved |Privacy Policy | Contact Us