JsonMappingException: No suitable constructor found for type [simple type, class ]: can not instantiate from JSON object(JsonMappingException:没有为类型 [简单类型,类] 找到合适的构造函数:无法从 JSON 对象实例化)
问题描述
我在尝试获取 JSON 请求并对其进行处理时收到以下错误:
I am getting the following error when trying to get a JSON request and process it:
org.codehaus.jackson.map.JsonMappingException:没有找到适合类型 [simple type, class com.myweb.ApplesDO] 的构造函数:无法从 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:
Here is the JSON I am trying to send:
{
"applesDO" : [
{
"apple" : "Green Apple"
},
{
"apple" : "Red Apple"
}
]
}
在Controller中,我有以下方法签名:
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:
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 对象.请帮助杰克逊将 JSON 转换为 Java 对象的配置参数.我正在使用 Spring 框架.
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.
在上述示例类中包含导致此问题的主要错误 - 请查看已接受的解决方案答案.
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 对象实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:JsonMappingException:没有为类型 [简单类型,类] 找到合适的构造函数:无法从 JSON 对象实例化


基础教程推荐
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 降序排序:Java Map 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01