JsonMappingException:找不到适合类型[简单类型,类]的构造函数:无法从JSON对象实例化尝试获取JSON请求并处理它时出现以下错误:
JsonMappingException
org.codehaus.jackson.map.JsonMappingException:未找到类型[简单类型,类com.myweb.ApplesDO]的合适构造函数:无法从JSON对象实例化(需要添加/启用类型信息吗?)
org.codehaus.jackson.map.JsonMappingException
这是我要发送的JSON:
{ "applesDO" : [ { "apple" : "Green Apple" }, { "apple" : "Red Apple" } ] }
在Controller中,我具有以下方法签名:
@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 Framework。
编辑:在上面的示例类中包括导致此问题的主要错误-请寻找已接受的答案作为解决方案。
所以,最后我意识到了问题所在。我怀疑这不是杰克逊的配置问题。
实际上问题出在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() { } }