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

查看:65
本文介绍了升级到 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 注释.这意味着该类没有无参数构造函数.

@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 的实例(没有创建者,如默认构造,存在):无法从对象值反序列化(没有基于委托或属性的创建者)

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.

我尝试在 Google 上搜索此问题,但只找到了 @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.20 中的重大更改,您需要在 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 问题,尽管它是关于 @Data:https://github.com/rzwitserloot/lombok/issues/1563

You may be interested in following the related GitHub issue here, although it's about @Data: https://github.com/rzwitserloot/lombok/issues/1563

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

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