[英]@NotNull and @NotEmpty for same property
I am using spring boot.我正在使用 spring 引导。
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
For same property I am giving two annotations one is for null check another is for empty check.对于相同的属性,我给出了两个注释,一个是 null 检查另一个是空检查。
@NotNull(message = "{email.notnull}")
@NotEmpty(message = "{email.notempty}")
private String email;
I am loading the error messages from properties file.我正在从属性文件加载错误消息。
When I give email as empty then I am getting the error message from email.notempty
only, but when I give email as null then email.notnull
and email.notempty
these two error messages are displaying, May I Know what is the mistake that I have done. When I give email as empty then I am getting the error message from email.notempty
only, but when I give email as null then email.notnull
and email.notempty
these two error messages are displaying, May I Know what is the mistake that I完成了。
I should be able to see only null error when I have given null.当我给出 null 时,我应该只能看到 null 错误。
As @NotEmpty
documentation states正如@NotEmpty
文档所述
The annotated element must not be null nor empty.带注释的元素不得为 null 也不得为空。
It has an implicit not null requirement so you can drop @NotNull
.它有一个隐含的非 null 要求,因此您可以删除@NotNull
。
See also Difference Between @NotNull, @NotEmpty, and @NotBlank Constraints in Bean Validation另请参阅Bean 验证中 @NotNull、@NotEmpty 和 @NotBlank 约束之间的区别
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.