傻傻分不清的 @NotEmpty、@NotBlank、@NotNull
序
区别:
- @NotEmpty 用在集合类上面
- @NotBlank 用在String上面
- @NotNull 用在基本类型上
只有简单的结果,但是再更具体一点的内容就搜不到了,所以去看了看源码,发现了如下的注释:
1. @NotEmpty
/**
* Asserts that the annotated string, collection, map or array is not {@code null} or empty.
*
* @author Emmanuel Bernard
* @author Hardy Ferentschik
*/
也就是说,加了 @NotEmpty 的 String 类、Collection、Map、数组,是不能为 null 或者长度为 0 的(String、Collection、Map 的 isEmpty() 方法)。
2. @NotBlank
/**
* Validate that the annotated string is not {@code null} or empty.
* The difference to {@code NotEmpty} is that trailing whitespaces are getting ignored.
*
* @author Hardy Ferentschik
*/
“The difference to {@code NotEmpty} is that trailing whitespaces are getting ignored.” –> 和 {@code NotEmpty} 不同的是,尾部空格被忽略,也就是说,纯空格的String也是不符合规则的。所以才会说@NotBlank用于String。
3. @NotNull
/**
* The annotated element must not be {@code null}.
* Accepts any type.
*
* @author Emmanuel Bernard
*/
这个就很好理解了,不能为null。
标题:傻傻分不清的 @NotEmpty、@NotBlank、@NotNull
作者:mmzsblog
地址:https://www.mmzsblog.cn/articles/2022/03/16/1647410693750.html
如未加特殊说明,文章均为原创,转载必须注明出处。均采用CC BY-SA 4.0 协议!
本网站发布的内容(图片、视频和文字)以原创、转载和分享网络内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。若本站转载文章遗漏了原文链接,请及时告知,我们将做删除处理!文章观点不代表本网站立场,如需处理请联系首页客服。• 网站转载须在文章起始位置标注作者及原文连接,否则保留追究法律责任的权利。
• 公众号转载请联系网站首页的微信号申请白名单!
