Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (4k points)

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 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> 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 a solution.

1 Answer

0 votes
by (46k points)

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

    }

}

Related questions

0 votes
1 answer
0 votes
1 answer
asked Oct 14, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer
+1 vote
1 answer
0 votes
1 answer

Browse Categories

...