简体   繁体   English

@NotNull 和 @NotEmpty 用于相同的属性

[英]@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.

相关问题 @NotEmpty 和 @notNull 在反序列化期间不起作用 - @NotEmpty and @notNull not working during deserialization 在Hibernate Validator 4.1+中,@ NotNull,@ NotEmpty和@NotBlank之间有什么区别? - In Hibernate Validator 4.1+, what is the difference between @NotNull, @NotEmpty, and @NotBlank? 支柱 <logic:notEmpty> 无法正常工作或bean属性未正确写入JSP? - Struts <logic:notEmpty> not working or bean property not being written properly to JSP? 为什么同时使用CheckForNull和NotNull? - Why is used CheckForNull and NotNull at the same time? 如果属性标记为 @NotNull,OAS 会忽略属性上的 @Schema(required=false) - OAS ignoring @Schema(required=false) on property if it marked as @NotNull 休眠@NotNull(仅当另一个属性不为null时?) - Hibernate @NotNull (only if another property isn't null?) @NotNull 约束不适用于应用程序属性值 spring 启动 - @NotNull constraint don't work for a application property value spring boot 您是否应该在同一对象中使用@NotNull和@JsonProperty(必需) - Should you use @NotNull and @JsonProperty(required) in the same object 列表不遵守@NotEmpty 注释 - List not honoring @NotEmpty annotation Java的。 JPA。 带有@ org.jetbrains.annotations.NotNull的@Entity类属性 - Java. JPA. @Entity class property with @org.jetbrains.annotations.NotNull
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM