spring 断言工具类Assert的使用

包:org.springframework.util.Assert;

Assert断言工具类,通常用于数据合法性检查.

一般判断数据是否为空 是这样写

if (message== null || message.equls("")) {  
    throw new IllegalArgumentException("输入信息错误!");  
} 

Assert断言工具类 可以直接这样写

Assert.hasText(String text, "xxxx输入错误") 

常用 方法
Assert.notNull(Object object, “object is required”) - 对象非空
Assert.isTrue(Object object, “object must be true”) - 对象必须为true
Assert.notEmpty(Collection collection, “collection must not be empty”) - 集合非空
Assert.hasLength(String text, “text must be specified”) - 字符不为null且字符长度不为0
Assert.hasText(String text, “text must not be empty”) - text 不为null且必须至少包含一个非空格的字符
Assert.isInstanceOf(Class clazz, Object obj, “clazz must be of type [clazz]”) - obj必须能被正确造型成为clazz 指定的类

发布了78 篇原创文章 · 获赞 5 · 访问量 7378

猜你喜欢

转载自blog.csdn.net/weixin_41930050/article/details/103031715