Java中@NotNull、@NotBlank和@NotEmpty

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值