javax.validation.constraints包下:
- @NotNull
The annotated element must not be {@code null}. Accepts any type.
对象不能为null,用在基本类型上 - @NotBlank
The annotated element must not be {@code null} and must contain at least one non-whitespace character. Accepts {@code CharSequence}.
对象不能为null,并且不能全是空格,用在String上 - @NotEmpty
The annotated element must not be {@code null} nor empty.
对象不能为null,并且不能为空,包括字符串类型,集合,map,Array。用着集合上
加入集合List使用@NotBlank,会报错: No validator could be found for constraint 'javax.validation.const。
cn.hutool.core.util包下:
StrUtil.isBlank
既判断str是否为null,又判断是否为空串,又判断trim()后是否为空
源码:
public static boolean isBlank(CharSequence str) {
int length;
if ((str == null) || ((length = str.length()) == 0)) {
return true;
}
for (int i = 0; i < length; i++) {
// 只要有一个非空字符即为非空字符串
if (false == CharUtil.isBlankChar(str.charAt(i))) {
return false;
}
}
return true;
}
StrUtil.isEmpty
既判断str是否为null,又判断是否为空串
源码:
public static boolean isEmpty(CharSequence str) {
return str == null || str.length() == 0;
}
StrUtil.isNullOrUndefined
检查字符串是否为null、“null”、“undefined”
源码:
public static boolean isNullOrUndefined(CharSequence str) {
if (null == str) {
return true;
}
return isNullOrUndefinedStr(str);
}
还有一些其他方法,如:
检查字符串是否为null、“”、“null”、“undefined”
StrUtil.isEmptyOrUndefined
检查字符串是否为null、空白串、“null”、“undefined”
StrUtil.isBlankOrUndefined
当给定字符串为空字符串时,转换为null
StrUtil.emptyToNull