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

3.1K    Asked by VanessaMarshall in Devops , Asked on Jul 1, 2021

 I am getting the following error “no suitable constructor found for type” when trying to get a JSON request and process it:

org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type [simple type, class com.myweb.ApplesDO]: can not instantiate from a JSON object (need to add/enable type information?)

Here is the JSON I am trying to send:

{
  "applesDO" : [
    {
      "apple" : "Green Apple"
    },
    {
      "apple" : "Red Apple"
    }
  ]
}
In Controller, I have the following method signature:
@RequestMapping("showApples.do")
public String getApples(@RequestBody final AllApplesDO applesRequest){
    // Method Code
}
AllApplesDO is a wrapper of ApplesDO :
public class AllApplesDO {
    private List applesDO;
    public List getApplesDO() {
        return applesDO;
    }
    public void setApplesDO(List applesDO) {
        this.applesDO = applesDO;
    }
}
ApplesDO:
public class ApplesDO {
    private String apple;
    public String getApple() {
        return apple;
    }
    public void setApple(String appl) {
        this.apple = apple;
    }
    public ApplesDO(CustomType custom){
        //constructor Code
    }
}

I think that Jackson is unable to convert JSON into Java objects for subclasses. Please help with the configuration parameters for Jackson to convert JSON into Java Objects. I am using Spring Framework.

EDIT: Included the major bug that is causing this problem in the above sample class - Please look accepted answer for a solution.

Answered by Neeraj Nair

The problem isn't with the Jackson configuration

Actually, the obstacle 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 set for the class executing it the default constructor. Preceding a dummy constructor has made the failure 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() {
    }
}

Your Answer

Answer (1)

The JsonMappingException: No Suitable Constructor Found For Type [Simple Type, Class ]: Can Not Instantiate From JSON Object error typically occurs in Java applications that use the Jackson library for JSON processing. This error indicates that Jackson is unable to find an appropriate constructor to instantiate an object of the specified class from the JSON data.

Here's a step-by-step approach to diagnose and fix this issue:

1. Ensure a No-Argument Constructor

Jackson requires a no-argument (default) constructor to instantiate objects. Ensure that your class has a public no-argument constructor.

Example:

  public class MyClass {    private String name;    private int age;    // Default constructor    public MyClass() {}    // Getters and setters    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }}

2. Use Jackson Annotations

If you have constructors with parameters, you can use Jackson annotations to specify how Jackson should handle them.

Example with @JsonCreator and @JsonProperty:

  • import com.fasterxml.jackson.annotation.JsonCreator;
  • import com.fasterxml.jackson.annotation.JsonProperty;

  public class MyClass {    private String name;    private int age;    @JsonCreator    public MyClass(@JsonProperty("name") String name, @JsonProperty("age") int age) {        this.name = name;        this.age = age;    }    // Getters and setters    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }}

3. Ensure Matching JSON Fields and Class Properties

Make sure the JSON fields match the class properties. Jackson maps JSON fields to class properties based on their names.


JSON Example:

  {    "name": "John",    "age": 30}

4. Check Access Modifiers

Ensure that the constructor and class are not private. Jackson needs to access the constructor to instantiate the class.

5. Register Modules

If you are using any custom modules or features (like the Java 8 DateTime module), ensure that they are registered with your ObjectMapper.

Example:

  ObjectMapper mapper = new ObjectMapper();mapper.registerModule(new JavaTimeModule());

6. Lombok

If you are using Lombok, ensure that you have a no-argument constructor or use the appropriate Lombok annotations.

Example 

with Lombok:

  import lombok.AllArgsConstructor;import lombok.NoArgsConstructor;import lombok.Data;
  @Data@NoArgsConstructor@AllArgsConstructorpublic class MyClass {    private String name;    private int age;}

Example Code for Fixing the Issue

Here is a complete example that includes a no-argument constructor and demonstrates how to deserialize a JSON string into a Java object:

  import com.fasterxml.jackson.annotation.JsonCreator;import com.fasterxml.jackson.annotation.JsonProperty;import com.fasterxml.jackson.databind.ObjectMapper;

public class MyClass {    private String name;    private int age;    // Default constructor    public MyClass() {}    // Constructor with parameters    @JsonCreator    public MyClass(@JsonProperty("name") String name, @JsonProperty("age") int age) {        this.name = name;        this.age = age;    }    // Getters and setters    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    public static void main(String[] args) throws Exception {        String json = "{"name":"John","age":30}";        ObjectMapper mapper = new ObjectMapper();        MyClass myClass = mapper.readValue(json, MyClass.class);        System.out.println("Name: " + myClass.getName());        System.out.println("Age: " + myClass.getAge());    }}Summary

Ensure a no-argument constructor exists.

Use @JsonCreator and @JsonProperty for parameterized constructors.

Check JSON and class property names match.

Make sure constructors and classes are not private.

Register any necessary Jackson modules.

If using Lombok, ensure proper annotations are used.

By following these guidelines, you should be able to resolve the JsonMappingException and successfully deserialize JSON objects into Java instances.








6 Months

Interviews

Parent Categories