本文翻译自:JsonMappingException: No suitable constructor found for type [simple type, class ]: can not instantiate from JSON object

I am getting the following error when trying to get a JSON request and process it: 我在尝试获取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?) org.codehaus.jackson.map.JsonMappingException:找不到类型[simple type,class com.myweb.ApplesDO]的合适构造函数:无法从JSON对象实例化(需要添加/启用类型信息?)

Here is the JSON I am trying to send: 这是我要发送的JSON:

{"applesDO" : [{"apple" : "Green Apple"},{"apple" : "Red Apple"}]
}

In Controller, I have the following method signature: 在Controller中,我有以下方法签名:

@RequestMapping("showApples.do")
public String getApples(@RequestBody final AllApplesDO applesRequest){// Method Code
}

AllApplesDO is a wrapper of ApplesDO : 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: 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}
}

I think that Jackson is unable to convert JSON into Java objects for subclasses. 我认为Jackson无法将JSON转换为子类的Java对象。 Please help with the configuration parameters for Jackson to convert JSON into Java Objects. 请帮助Jackson将JSON转换为Java Objects的配置参数。 I am using Spring Framework. 我正在使用Spring Framework。

EDIT: Included the major bug that is causing this problem in the above sample class - Please look accepted answer for solution. 编辑:包括在上面的示例类中导致此问题的主要错误 - 请查看接受的解决方案的答案。


#1楼

参考:https://stackoom.com/question/VzoV/JsonMappingException-找不到类型-simple-type-class-的合适构造函数-无法从JSON对象实例化


#2楼

This happens for these reasons: 出现这种情况的原因如下:

  1. your inner class should be defined as static 你的内部类应该定义为静态

     private static class Condition { //jackson specific } 
  2. It might be that you got no default constructor in your class ( UPDATE: This seems not to be the case) 可能是你的类中没有默认构造函数( 更新:似乎不是这种情况)

     private static class Condition { private Long id; public Condition() { } // Setters and Getters } 
  3. It could be your Setters are not defined properly or are not visible (eg private setter) 可能是您的Setter未正确定义或不可见(例如私人制定者)


#3楼

I would like to add another solution to this that does not require a dummy constructor. 我想为此添加另一个不需要虚拟构造函数的解决方案。 Since dummy constructors are a bit messy and subsequently confusing. 因为虚拟构造函数有点混乱并且随后令人困惑。 We can provide a safe constructor and by annotating the constructor arguments we allow jackson to determine the mapping between constructor parameter and field. 我们可以提供一个安全的构造函数,并通过注释构造函数参数,我们允许jackson确定构造函数参数和字段之间的映射。

so the following will also work. 所以以下也会奏效。 Note the string inside the annotation must match the field name. 请注意,注释中的字符串必须与字段名称匹配。

import com.fasterxml.jackson.annotation.JsonProperty;
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 ApplesDO(@JsonProperty("apple")String apple) {}}

#4楼

Thumb Rule : Add a default constructor for each class you used as a mapping class. Thumb Rule :为您用作映射类的每个类添加默认构造函数。 You missed this and issue arise! 你错过了这个,问题就出现了!
Simply add default constructor and it should work. 只需添加默认构造函数即可。


#5楼

When I ran into this problem, it was a result of trying to use an inner class to serve as the DO. 当我遇到这个问题时,它是尝试使用内部类作为DO的结果。 Construction of the inner class (silently) required an instance of the enclosing class -- which wasn't available to Jackson. 构建内部类(默默地)需要一个封闭类的实例 - 杰克逊无法获得。

In this case, moving the inner class to its own .java file fixed the problem. 在这种情况下,将内部类移动到其自己的.java文件可以解决问题。


#6楼

Failing custom jackson Serializers/Deserializers could also be the problem. 失败的自定义jackson序列化器/解串器也可能是问题所在。 Though it's not your case, it's worth mentioning. 虽然不是你的情况,但值得一提。

I faced the same exception and that was the case. 我遇到了同样的例外情况。

JsonMappingException:找不到类型[simple type,class]的合适构造函数:无法从JSON对象实例化相关推荐

  1. java中的类型擦除type erasure

    文章目录 简介 举个例子 原因 解决办法 总结 java中的类型擦除type erasure 简介 泛型是java从JDK 5开始引入的新特性,泛型的引入可以让我们在代码编译的时候就强制检查传入的类型 ...

  2. java 类型擦除_java中的类型擦除type erasure

    简介 泛型是java从JDK 5开始引入的新特性,泛型的引入可以让我们在代码编译的时候就强制检查传入的类型,从而提升了程序的健壮度. 泛型可以用在类和接口上,在集合类中非常常见.本文将会讲解泛型导致的 ...

  3. Type definition error: [simple type, class java.time.Instant]; nested exception is com.fasterxml.jac

    postman调用接口报错 "type": "https://www.jhipster.tech/problem/problem-with-message", ...

  4. 找不到类型或命名空间名称[重复]

    本文翻译自:The type or namespace name could not be found [duplicate] This question already has an answer ...

  5. linux 进程间通信 dbus-glib【实例】详解三 数据类型和dteeth(类型签名type域)(层级结构:服务Service --> Node(对象、object) 等 )(附代码)

    linux 进程间通信 dbus-glib[实例]详解一(附代码)(d-feet工具使用) linux 进程间通信 dbus-glib[实例]详解二(上) 消息和消息总线(附代码) linux 进程间 ...

  6. 找不到类型{0} 它在 ServiceHost 指令中提供为 Service 特性值

    由于我把binding改成wsHttpBinding,在web.config里也改了命名空间 services的类名也改成了跟 web.config对应的命名空间后 在添加引用后,出现了错误: &qu ...

  7. 不完整类型(partial type)

    一.不完整类型(partial type)实例 1 public partial class Program 2 { 3 public void BaseFunc() 4 { 5 Console.Wr ...

  8. C# 8中的范围类型(Range Type)

    C# 8.0中加入了一个新的范围类型(Range Type). 这里我们首先展示一些代码,并一步一步为代码添加一些不同的东西, 为大家展示一下范围类型的功能和用法. 我们最原始的代码如下: 这里我们显 ...

  9. TypeScript 交叉类型(intersection type)

    在TS中和联合类型(union type)对应的还有交叉类型(intersection type). 交叉类型的出现主要为了组合多个对象类型(object type),因为相对于interface,o ...

最新文章

  1. 绝对不容错过:最完整的检测模型评估指标mAP计算指南(附代码)在这里!
  2. php 添加内链插件,ecshop自动内链插件seo优化插件
  3. win7完整卸载oracle11,win7安装与卸载oracle11g(32)
  4. 【网址收藏】安装VMware Tools显示灰色正确解决办法
  5. 再见!妈妈再也不用担心我的计算机基础!
  6. 有关Accordion组件的研究——Silverlight学习笔记[27]
  7. tomcat调优方案
  8. 德国软件巨头SAP旗下风投基金募集10亿美元 专门投资科技初创公司
  9. Android开发笔记(一百二十四)自定义相册
  10. Python获取图片的大小/尺寸
  11. 如何彻底关闭windows10自动更新
  12. Spring揭秘(一)spring框架的由来
  13. 非极大值抑制(Non-Maximum Suppression,NMS)
  14. innerHTML、innerText 和 outerHTML 区别
  15. ffmpeg合并多mp4视频
  16. 软件压力测试的手段有注入错误吗,软件错误注入测试技术研究_陈锦富.pdf
  17. linux lsiutil raid创建,使用Lsiutil管理Dell SAS 6i/R
  18. python引用类全局变量_调用全局变量时无法解析的引用? - python
  19. flutter引入高德地图_玩玩Flutter Web —— 实现高德地图插件
  20. 盒子阴影 – box-shadow

热门文章

  1. C语言基本数据结构之四(线性,对分,二叉树查找及二叉树删除)
  2. Java IO 体系(三):Reader与Writer
  3. [置顶] Android输入输出机制之来龙去脉之前生后世
  4. Android 页面进行镜像反转-面试
  5. 算法--------------------寻找两个有序数组的中位数
  6. android GLSurfaceView渲染模式
  7. wireshark网络分析就这么简单_【读书笔记】2wireshark网络分析就这么简单——不同子网如何发送消息。...
  8. python实训收获_英泰移动通信:学习Python心得体会
  9. PHP学习笔记-PHP语言基础3
  10. Android之工程目录介绍