摘要

记录下使用lombok遇到的反序列问题,一开始在lombok1.16.18中并没有发现,然后应用中没有指定lombok全局版本,引入的其他二方包将lombok版本提升到了1.16.20,然后报错。因为这个问题需要允许时才能发现,很可能会造成线上故障,所以不能等到出现问题时才发现,需要提前知晓。

错误栈

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.fs.jackson.Address` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)at [Source: (String)"{"id":1,"address":"address"}"; line: 1, column: 2]at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:67)at com.fasterxml.jackson.databind.DeserializationContext.reportBadDefinition(DeserializationContext.java:1451)at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1027)at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1297)at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:326)at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:159)at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013)at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3004)at Application.testSeriable(Application.java:36)at Application.run(Application.java:24)at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:790)at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:774)at org.springframework.boot.SpringApplication.run(SpringApplication.java:335)at org.springframework.boot.SpringApplication.run(SpringApplication.java:1246)at org.springframework.boot.SpringApplication.run(SpringApplication.java:1234)at Application.main(Application.java:27)

问题

问题1:

手贱把一个private修饰符写成了final@Data
public class AddressVO {final Long id;private String address;
}

问题2 使用了Bulider类

@Data
@Accessors(chain = true)
@Builder
public class Address {private Long id;private String address;
}

分析

分析lombok导致的问题很简单,查看下编译后的class文件即可。
使用jd-gui反编译工具查看一下

1.6.18

public class AddressByBuilder {private Long id;private String address;public static class AddressBuilder {private Long id;private String address;// 省略}@ConstructorProperties({ "id", "address" })AddressByBuilder(Long id, String address) {this.id = id;this.address = address;}
}

1.16.20

public class AddressByLombok20 {private Long id;private String address;// 构造函数AddressByLombok20(Long id, String address) {this.id = id;this.address = address;}
}

对比1.16.28与1.16.20发现构造函数发生了变化。1.16.20中构造函数少了@ConstructorProperties({ "id", "address" })
JDK1.6中出来的

The annotation shows that the first parameter of the constructor can be retrieved with the getX() method and the second with the getY() method. Since parameter names are not in general available at runtime, without the annotation there would be no way to know whether the parameters correspond to getX() and getY() or the other way around.

@ConstructorProperties()注解用于构造函数上,表示构造函数可以通过GetName来找到,第一个参数可以使用getX()方法检索,第二个参数可以使用方法检索getY()。由于方法参数名一般在运行时不可见,如果没有标注就没有办法知道参数是否符合getX() 和getY()或周围的其他方法。

这显然是lombok升级过程中的一个不兼容的改造。

因为我们都没有定义无参构造函数,所以会找已有的构造函数,然后匹配getter/setter函数。

解决

1.maven中指定lombok固定版本,使用1.16.18版本,代码层面不需要做更改
2.类中添加无参构造器

建议

需要序列化的类,比如与前端交互,rpc调用,都加上无参构造器,兼容性比较好

参考

http://tutorials.jenkov.com/java-json/jackson-objectmapper.html#how-jackson-objectmapper-matches-json-fields-to-java-fields、
https://www.jianshu.com/p/f826ba2dbcf9

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct (no Creators)相关推荐

  1. com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.zyw

    问题如下 com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of com ...

  2. com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `

    一 问题: Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct ins ...

  3. com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.lxw

    使用Feign是出现的问题 SpringCloud对Feign进行了增强兼容了SpringMVC的注解 ,我们在使用SpringMVC的注解时需要注意: 1.feignClient接口 有参数在参数必 ...

  4. com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.zha

    在使用SpringCloud进行RestTemplate 反序列的时候报错 解决方案: 认真核对两个实体类中的字段是否相同即可解决 细心查找不行就直接复制 过去 最好是复制 以免出错

  5. com.fasterxml.jackson.databind.exc.InvalidDefinitionException

    com.fasterxml.jackson.databind.exc.InvalidDefinitionException: 错误信息 com.fasterxml.jackson.databind.e ...

  6. java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.exc.InvalidDefinitionException

    在引入Jacksonjar包时,运行程序抛出了这个异常: java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.exc.In ...

  7. 报错,nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException??

    1. 问题: 2021-01-06 15:43:41.663 ERROR 10184 --- [io-13000-exec-2] c.y.a.c.v1.retcode.RetControlller   ...

  8. com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `x`

    记录一下有点蠢的问题,序列化再次序列化就会出现这种问题. [2022-05-16 10:18:18.645] [ERROR] com.fu.common.global.GlobalExceptionH ...

  9. jackson序列化错误 get类型方法名的坑 [com.fasterxml.jackson.databind.exc.InvalidDefinitionException]

    错误:com.fasterxml.jackson.databind.exc.InvalidDefinitionException com.fasterxml.jackson.databind.exc. ...

最新文章

  1. 微信小程序地图的实现
  2. 深度学习(7)卷积神经网络
  3. bfs迷宫寻路问题(一看就懂的讲解)
  4. php和python性能-Node.js与PHP、Python的字符处理性能对比
  5. cnpm与npm指定有什么区别?
  6. 12款白帽子用于黑客渗透测试的操作系统
  7. php pacs,DICOM医学图像处理:WEB PACS初谈四,PHP DICOM Class – 只要踏出一步,路就在前方——zssure – CSDN博客...
  8. 潭州课堂25班:Ph201805201 django 项目 第二十四课 文章主页 多级评论数据库设计 ,后台代码完成 (课堂笔记)...
  9. SharePoint 2010列表中新增的唯一性验证
  10. 广播接收器动态静态注册
  11. pyhive、pyspark配置
  12. 笔记本检测工具(全)
  13. 脉冲触发器和边沿触发器的理解(移位寄存器采用边沿触发)
  14. / ./ ../相对路径详细解释
  15. 阿里云的服务器居然泡在“水”里?| 数据中心参观有感
  16. popen 的使用方法及场景
  17. php作为客户端websocket,使用PHP客户端连接到websocket
  18. WPS添加页码不是从首页开始
  19. 关于model层建VO和PO,实体类(VO,DO,DTO)的划分
  20. 14 Java集合(集合框架+泛型+ArrayList类+LinkedList类+Vector类+HashSet类等)

热门文章

  1. Linux man手册的基本组成和使用方法
  2. STM32 DFU模式烧录代码
  3. 腾创秒会达MHD视频会议系统
  4. pg9.4 VS pg12大表join
  5. Python 列表和元组的区别是什么?
  6. 家装行业如何进行客户关系管理?
  7. 不能分配USB设备Generic USB3.0 Card Reader[]到虚拟电脑Ubantu
  8. 【echarts】水球图
  9. python精灵模块示例代码
  10. 如果生命即将结束 你会做些什么?