问题描述
我正试图获得一个JSON请求并处理它>如果出现以下错误
I am getting the following error when trying to get a JSON request and process it>
组织codehaus.jackson.map.JsonMappingException:找到了类型没有合适的构造函数[简单类型,类com.myweb.ApplesDO]:无法从JSON对象实例化(需要添加/启用类型的信息?)
下面是JSON我想送:
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是ApplesDO的包装:
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
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
}
}
我想,杰克逊无法JSON转换成Java对象为sublclasses。请与配置参数杰克逊JSON转换成Java对象的帮助!我使用Spring框架
I am thinking that JACKSON is unable to convert JSON into JAVA objects for sublclasses. Please help with the configuration parameters for JACKSON to convert JSON into JAVA Objects! I am using Spring Framework
编辑:包括是导致在上面的示例类这一问题的主要错误 - 敬请期待解决方案接受的答案
Included the major bug that is causing this problem in the above sample class - Please look accepted answer for solution.
推荐答案
所以,最后我意识到的问题是什么。这不是杰克逊的配置问题,因为我怀疑。
So, finally I realized what the problem is. It is not a Jackson configuration issue as I doubted.
其实这个问题在 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
}
}
有是使得它的默认构造函数的类定义的自定义构造函数。引入一个虚拟的构造做出了错误走开:
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() {
}
}
这篇关于JsonMappingException:找到了类型没有合适的构造函数[简单类型,等级]:无法从JSON对象实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!