JsonMappingException:找不到适用于类型[simple type,class]的构造函数:无法从JSON对象中实例化

问题:

尝试获取JSON请求并处理它时,我收到以下错误:

org.codehaus.jackson.map.JsonMappingException:找不到适合的类型[simple type,class com.myweb.ApplesDO]的构造函数:无法从JSON对象中实例化(需要添加/启用类型信息?)

这是我要发送的JSON:
class=”lang-json prettyprint-override”>

{
  "applesDO" : [
    {
      "apple" : "Green Apple"
    },
    {
      "apple" : "Red Apple"
    }
  ]
}

在控制器中,我有以下方法签名:

@RequestMapping("showApples.do")
public String getApples(@RequestBody final AllApplesDO applesRequest){
    // Method Code
}

AllApplesDO是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
    }
}

我认为Jackson无法将JSON转换为Java对象的子类。请帮助Jackson的配置参数将JSON转换为Java对象。我正在使用Spring框架。
编辑:包括在上面的示例类中引起此问题的主要错误 – 请看看解答的接受答案。

回答:

所以,最后我意识到问题是什么。我怀疑这不是Jackson配置问题。
其实问题出在ApplesDO类:

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

}

 
 
Code问答: http://codewenda.com/topics/python/
Stackoverflow: JsonMappingException: No suitable constructor found for type [simple type, class ]: can not instantiate from JSON object

*转载请注明本文链接以及stackoverflow的英文链接

发表评论

电子邮件地址不会被公开。 必填项已用*标注

28 − = 18