@NotEmpty、@NotNull、@NotBlank的源码注释、区别

@NotEmpty

/** 
* Asserts that the annotated string, collection, map or array is not {@code null} or empty. 
* 
* @author Emmanuel Bernard 
* @author Hardy Ferentschik 
*/

翻译过来是:断言,带注解的String,collection,map或数组不能为null,也不能为空。
也就是只对上述4种类型参数进行判断,判断条件:obj != null && obj.size() > 0。

@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 
*/

翻译过来是:验证注释的String不是null或空的,与@NotEmpty的区别在于,尾部空格被忽略了(也就是说,纯空格的String也是不符合规则的)。
此注解只能用于验证String类型,判断条件:str != null && str.trim().size() > 0。

@NotNull

/** 
* The annotated element must not be {@code null}. 
* Accepts any type. 
* 
* @author Emmanuel Bernard 
*/

翻译过来是:带注释的元素不能为null。接受任何类型。
它只判断是否是null。

最后

@NotNull:任何地方都可以用,但是它只判断是否是null
@NotEnpty:主要用于String,collection,map或数组,判断是否是null并且size>0
@NotBlank: 只用于String,会先个str执行trim(),再判断是否为null并且size>0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值