示例:
String name = null;
@NotNull: false
@NotEmpty: false
@NotBlank: false
String name = "";
@NotNull: true
@NotEmpty: false
@NotBlank: false
String name = " ";
@NotNull: true
@NotEmpty: true
@NotBlank: false
String name = "Great answer!";
@NotNull: true
@NotEmpty: true
@NotBlank: true
简述三者区别
@NotNull://CharSequence, Collection, Map 和 Array 对象不能是 null, 但可以是空集(size = 0)。
@NotEmpty://CharSequence, Collection, Map 和 Array 对象不能是 null 并且相关对象的 size 大于 0。
@NotBlank://String 不能是 null 且去除两端空白字符后的长度(trimmed length)大于 0。
注解的定义(在version 4.1中):
1、@NotNull:
定义如下:
| 1 |
|
这个类中有一个isValid方法是这么定义的:
| 1 2 3 |
|
对象不是null就行,其他的不保证。
2、@NotEmpty:
定义如下:
| 1 2 |
|
也就是说,@NotEmpty除了@NotNull之外还需要保证@Size(min=1),这也是一个注解,这里规定最小长度等于1,也就是类似于集合非空。
3、@NotBlank:
| 1 2 |
|
类似地,除了@NotNull之外,还有一个类的限定,这个类也有isValid方法:
| 1 2 3 4 |
|
| 1 |
|
