JsonMappingException:找不到适合类型 [简单类型,类] 的构造函数:无法从 JSON 对象实例化 [英] JsonMappingException: No suitable constructor found for type [simple type, class ]: can not instantiate from JSON object

查看:44
本文介绍了JsonMappingException:找不到适合类型 [简单类型,类] 的构造函数:无法从 JSON 对象实例化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试获取 JSON 请求并对其进行处理时出现以下错误:

I am getting the following error when trying to get a JSON request and process it:

org.codehaus.jackson.map.JsonMappingException:找不到适合类型 [简单类型,类 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"
    }
  ]
}

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

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

苹果:

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 Objects 的配置参数.我正在使用 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.

推荐答案

所以,我终于意识到问题所在.这不是我怀疑的 Jackson 配置问题.

So, finally I realized what the problem is. It is not a Jackson configuration issue as I doubted.

实际上问题出在 ApplesDO 类中:

Actually the problem was in ApplesDO Class:

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 对象实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆