Lombok,Spring mongodb 和 jackson 构造函数问题

Lombok, Spring mongodb and jackson Constructor problem

我在我的项目中使用 Spring Boot 和 lombok,但遇到了一些问题。 我的 class 看起来像这样:

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import lombok.Data;

@Data
@Document(collection = "elements")
public class ElementEntity {

    @Id
    private String id;
    // ...
}

现在,如果我使用 jackson ObjectMapper 创建我的 ElementEntity,我会收到以下运行时错误:

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

但是如果我从 lombok 添加 @NoArgsConstructor,我会得到以下编译错误:

[ERROR] ElementEntity.java:[11,1] constructor ElementEntity() is already defined in class ElementEntity

似乎 @Document 添加了一个,但可能仅具有包可见性。有没有简单的方法来解决这个问题,或者我必须手动添加一个 public no args constructor to every @Document?

尝试将 id 字段定义更改为:

@Id
@Getter
@Setter
private String id;

这是 lombok 1.16.22 的一个错误,请尝试升级到 1.18.0,

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.0</version>
    <scope>provided</scope>
</dependency>

Read