源碼解析
- @NotEmpty根據(jù)JDK源碼注釋說明,該注解只能應(yīng)用于char可讀序列(可簡單理解為String對象),colleaction,map,array上幅聘,因?yàn)樵撟⒔庖蟮氖菍ο蟛粸閚ull且size>0凡纳,所以只有上述對象是擁有size屬性的,而Integer,Long等基礎(chǔ)對象包裝類沒有該屬性
/**
* 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的實(shí)現(xiàn)類有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> 數(shù)組
* </ul>
*/
- @NotNull,表示不能為null荐糜,但可以為empty,與@NotEmpty注解相比是少了size屬性,所以"Accepts any type"可以接受任何類型對象
/**
* The annotated element must not be {@code null}.
* Accepts any type.
*/
- @NotBlank皆撩,"Accepts {@code CharSequence}"表明只應(yīng)用于char值可讀序列宾娜,則可以簡單理解為只用于String拟蜻,且不能為null伐憾,"non-whitespace"表示不能是空白字符捐凭,所以校驗(yàn)字符串是調(diào)用trim()方法之后的字符串長度大于0
/**
* The annotated element must not be {@code null} and must contain at least one
* non-whitespace character. Accepts {@code CharSequence}.
*/