当前位置: 移动技术网 > IT编程>开发语言>Java > @NotEmpty、@NotBlank、@NotNull的区别

@NotEmpty、@NotBlank、@NotNull的区别

2019年07月22日  | 移动技术网IT编程  | 我要评论

在网上搜索的内容,大致如下:

验证框中@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。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网