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

456,512

Solution 1

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() {
    }

}

Solution 2

This happens for these reasons:

  1. your inner class should be defined as static

    private static class Condition {  //jackson specific    
    }
    
  2. It might be that you got no default constructor in your class (UPDATE: This seems not to be the case)

    private static class Condition {
        private Long id;
    
        public Condition() {
        }
    
        // Setters and Getters
    }
    
  3. It could be your Setters are not defined properly or are not visible (e.g. private setter)

Solution 3

I would like to add another solution to this that does not require a dummy constructor. Since dummy constructors are a bit messy and subsequently confusing. We can provide a safe constructor and by annotating the constructor arguments we allow jackson to determine the mapping between constructor parameter and field.

so the following will also work. Note the string inside the annotation must match the field name.

import com.fasterxml.jackson.annotation.JsonProperty;
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
        }

        public ApplesDO(@JsonProperty("apple")String apple) {
        }

}

Solution 4

When I ran into this problem, it was a result of trying to use an inner class to serve as the DO. Construction of the inner class (silently) required an instance of the enclosing class -- which wasn't available to Jackson.

In this case, moving the inner class to its own .java file fixed the problem.

Solution 5

Generally, this error comes because we don’t make default constructor.
But in my case:
The issue was coming only due to I have made used object class inside parent class.
This has wasted my whole day.

Share:
456,512
Lucky Murari
Author by

Lucky Murari

Updated on August 12, 2020

Comments

  • Lucky Murari
    Lucky Murari over 3 years

    I am getting the following error 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 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> applesDO;
    
        public List<ApplesDO> getApplesDO() {
            return applesDO;
        }
    
        public void setApplesDO(List<ApplesDO> 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 solution.