gpt4 book ai didi

java - 相同属性的@NotNull 和@NotEmpty

转载 作者:行者123 更新时间:2023-12-05 02:43:58 26 4
gpt4 key购买 nike

我正在使用 Spring Boot 。

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

对于同一个属性,我给出了两个注释,一个用于空检查,另一个用于空检查。

@NotNull(message = "{email.notnull}")
@NotEmpty(message = "{email.notempty}")
private String email;

我正在加载属性文件中的错误消息。

当我将电子邮件设置为 empty 时,我只会从 email.notempty 收到错误消息,但是当我将电子邮件设置为 null 时,则 email.notnull email.notempty 这两个错误消息正在显示,请问我犯了什么错误。

当我给出 null 时,我应该只能看到 null 错误。

最佳答案

作为@NotEmpty documentation

The annotated element must not be null nor empty.

它有一个隐含的非空要求,因此您可以删除 @NotNull

另见 Difference Between @NotNull, @NotEmpty, and @NotBlank Constraints in Bean Validation

关于java - 相同属性的@NotNull 和@NotEmpty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66685547/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com