例子:
String str = null; //都不能为null
@NotNull: false
@NotEmpty: false
@NotBlank: false
String str = "";
@NotNull: true //可以为空 没有长度限制
@NotEmpty: false
@NotBlank: false
String str = " ";
@NotNull: true
@NotEmpty: true //长度为1 不除去空格
@NotBlank: false //用trim()后 长度为0
String str = "Hello!";
@NotNull: true
@NotEmpty: true
@NotBlank: true
1、@NotEmpty
/**
* The annotated element must not be {@code null} nor empty. Supported types are:
* <ul>
* <li>{@code CharSequence} (length of character sequence is evaluated)</li>
* <li>{@code Collection} (collection size is evaluated)</li>
* <li>{@code Map} (map size is evaluated)</li>
* <li>Array (array length is evaluated)</li>
* </ul>
*
* @author Emmanuel Bernard
* @author Hardy Ferentschik
*
* @since 2.0
*/
@NotEmpty注解的String、Collection、Map、数组不能为null或者长度为0(纯空格的String符合规则)
2、@NotBlank
/**
* The annotated element must not be {@code null} and must contain at least one
* non-whitespace character. Accepts {@code CharSequence}.
*
* @author Hardy Ferentschik
* @since 2.0
*
* @see Character#isWhitespace(char)
*/
@NotBlank注释的String不能为null或空 (尾部空格被忽略,纯空格的String也是不符合规则。即trim()之后,要length>0才符合)
3、@NotNull
/**
* The annotated element must not be {@code null}.
* Accepts any type.
*
* @author Emmanuel Bernard
*/
@NotNull 注释的元素不能为null,但可以为empty,没有长度的约束。接受任何类型