升级到Spring Boot 2后,ObjectMapper无法在没有默认构造函数的情况下反序列化 [英] ObjectMapper can't deserialize without default constructor after upgrade to Spring Boot 2

查看:738
本文介绍了升级到Spring Boot 2后,ObjectMapper无法在没有默认构造函数的情况下反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下DTO:

@Value
public class PracticeResults {
    @NotNull
    Map<Long, Boolean> wordAnswers;
}

@Value
public class ProfileMetaDto {

    @NotEmpty
    String name;
    @Email
    String email;
    @Size(min = 5)
    String password;
}

@Value是一个生成构造函数的Lombok注释。这意味着这个类没有no-arg构造函数。

@Value is a Lombok annotation which generates a constructor. Which means that this class doesn't have a no-arg constructor.

我使用Spring Boot 1.4.3.RELEASE和ObjectMapper bean能够从json反序列化这样的对象。

I used Spring Boot 1.4.3.RELEASE and ObjectMapper bean was able to deserialize such object from json.

升级到Spring Boot 2.0.0.M7后,我收到以下异常:

After the upgrade to Spring Boot 2.0.0.M7 I receive following exception:

com.fasterxml.jackson.databind.exc.InvalidDefinitionException:无法构造PracticeResults的实例(没有Creator,如默认构造,存在):无法从Object值反序列化(没有基于委托或属性的Creator)

Spring Boot 1.4.3中使用的Jackson版本是 2.8.10 和Spring Boot 2.0。 0.M7 2.9.2

Jackson version used in Spring Boot 1.4.3 is 2.8.10 and for Spring Boot 2.0.0.M7 is 2.9.2.

我试过谷歌这个问题,但发现只有 @JsonCreator @JsonProperty

I've tried to Google this problem but found only solutions with @JsonCreator or @JsonProperty.

那么,为什么呢它适用于Spring Boot 1.4.3并且在Spring Boot 2中失败了?是否可以将bean配置为与旧版本相同的方式?

So, why does it work with Spring Boot 1.4.3 and fails with Spring Boot 2? Is it possible to configure bean to behave the same way as the older version?

推荐答案

由于Lombok版本1.16中的更改。您需要在 lombok.config 文件中设置以下属性(如果您没有此文件,则可以在项目根目录中创建它):

Due to breaking changes in Lombok version 1.16.20 you need to set the following property in your lombok.config file (if you don't have this file you can create it in your project root):

lombok.anyConstructor.addConstructorProperties=true

这在Lombok更改日志中有所描述: https://projectlombok.org/changelog

This is described in the Lombok changelog: https://projectlombok.org/changelog.

之后,杰克逊应再次接受@Value。

After that the @Value should be accepted again by Jackson.

您可能有兴趣关注相关的GitHub问题在这里: https://github.com/rzwitserloot/lombok/issues/1563

You may be interested in following the related GitHub issue here: https://github.com/rzwitserloot/lombok/issues/1563

这篇关于升级到Spring Boot 2后,ObjectMapper无法在没有默认构造函数的情况下反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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