java – JsonMappingException:没有为类型[简单类型,类]找到合适的构造函数:不能从JSON对象实例化

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

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?)

这里是我想要发送的JSON:

{
  "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;
    }
}

苹果DO:

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

我认为杰克逊无法将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() {
    }

}
http://stackoverflow.com/questions/7625783/jsonmappingexception-no-suitable-constructor-found-for-type-simple-type-class

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:java – JsonMappingException:没有为类型[简单类型,类]找到合适的构造函数:不能从JSON对象实例化