源碼解析
- @NotEmpty根據JDK源碼注釋說明,該注解只能應用於char可讀序列(可簡單理解為String對象),colleaction,map,array上,因為該注解要求的是對象不為null且size>0,所以只有上述對象是擁有size屬性的,而Integer,Long等基礎對象包裝類沒有該屬性
/**
* The annotated element must not be {@code null} nor empty. Supported types are:
* <ul>
* <li>{@code CharSequence} (length of character sequence is evaluated)</li> char值得可讀序列,CharSequence的實現類有String, StringBuffer, StringBuilder, CharBuffer
* <li>{@code Collection} (collection size is evaluated)</li> 集合類
* <li>{@code Map} (map size is evaluated)</li> map散列表
* <li>Array (array length is evaluated)</li> 數組
* </ul>
*/
- @NotNull,表示不能為null,但可以為empty,與@NotEmpty注解相比是少了size屬性,所以"Accepts any type"可以接受任何類型對象
/**
* The annotated element must not be {@code null}.
* Accepts any type.
*/
- @NotBlank,"Accepts {@code CharSequence}"表明只應用於char值可讀序列,則可以簡單理解為只用於String,且不能為null,"non-whitespace"表示不能是空白字符,所以校驗字符串是調用trim()方法之后的字符串長度大於0
/**
* The annotated element must not be {@code null} and must contain at least one
* non-whitespace character. Accepts {@code CharSequence}.
*/