Skip to content
标签
欢迎扫码关注公众号

Lombok @Data & @NotNull & @Validated 错误消息重复

今天发现 @NotNull 注解对应的错误消息重复了两遍,调试发现 ex.getBindingResult().getFieldErrors() 中有两个相同的 Error

仔细对比后发现两个 Error 对应的 violation.elementType 不一样,一个是 METHOD ,一个是 FIELD 。这才想起来去看一下 Lombok 自动生成的代码,发现 fieldgetter 上各有一个 NotNull 注解。

POJO 代码:

java
/**
  * 用户类型(1:用户;2:商家;)
  */
@ApiModelProperty("用户类型(1:用户;2:商家;)")
@NotNull(message = "用户类型不能为空")
@Range(min = 1, max = 2, message = "错误的用户类型")
private Integer userType;

Lombok 自动生成的代码:

java
@ApiModelProperty("用户类型(1:用户;2:商家;)")
private @NotNull(
    message = "用户类型不能为空"
) @Range(
    min = 1L,
    max = 2L,
    message = "错误的用户类型"
) Integer userType;

public @NotNull(
    message = "用户类型不能为空"
) Integer getUserType() {
    return this.userType;
}

项目中使用的 Lombok 版本为 1.18.24 ,将其修改为 1.18.22 后生成的 class 代码中 getter 方法上就没有 @NotNull 注解了。

java
@ApiModelProperty("用户类型(1:用户;2:商家;)")
private @NotNull(
    message = "用户类型不能为空"
) @Range(
    min = 1L,
    max = 2L,
    message = "错误的用户类型"
) Integer userType;

public Integer getUserType() {
    return this.userType;
}

官方仓库上搜了一下,怀疑是这个 PR(Update NONNULL_ANNOTATIONS and BASE_COPYABLE_ANNOTATIONS)导致的。

根据 PR 的回复,可以看到已经导致了一些问题。暂时的解决方案就是在没有修复之前,最好使用 1.18.22 或更早的版本。


最近新项目用的 1.18.26 版,已经没有这个问题了。

Page Layout Max Width

Adjust the exact value of the page width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the page layout
A ranged slider for user to choose and customize their desired width of the maximum width of the page layout can go.

Content Layout Max Width

Adjust the exact value of the document content width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the content layout
A ranged slider for user to choose and customize their desired width of the maximum width of the content layout can go.