Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
934 views
in Technique[技术] by (71.8m points)

java - ObjectMapper can't deserialize without default constructor after upgrade to Spring Boot 2

I have following DTOs:

@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 is a Lombok annotation which generates a constructor. Which means that this class doesn't have a no-arg constructor.

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

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

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of PracticeResults (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)

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.

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...