spring的断言工具类Assert的基本使用
这几天比较闲看了下以前的项目,发现了这个spring下的Assert方法,(以前用过,不过好像忘的差不多了*.*)
org.springframework.util.Assert;
Assert断言工具类,通常用于数据合法性检查.
平时做判断通常都是这样写
if (message== null || message.equls("")) {
throw new IllegalArgumentException("输入信息错误!");
}
用Assert工具类上面的代码可以简化为:
Assert.hasText((message, "输入信息错误!");
下面来介绍我收集的一下Assert 类中的常用断言方法:
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 指定的类
相关文章
- spring注解工具类AnnotatedElementUtils和AnnotationUtils
- spring security实战项目笔记-用户管理
- Java 必须掌握的 12 种 Spring 常用注解!
- Arthas(阿尔萨斯)- 阿里开源的Java在线诊断工具(火焰图-实战篇--spring copyproperties方法)
- spring-boot 几个工具类(七)
- Spring的注解@Qualifier注解
- 可以随时拿取spring容器中Bean的工具类
- Spring 的优秀工具类盘点
- SpringBoot日志logback-spring.xml分环境(转)
- Spring Autowired原理
- Spring Boot 内置工具类
- 基于Spring-AOP的自定义分片工具
- 【微服务~原始真解】Spring Cloud —— 分布式配置组件
- 【微服务~原始真解】Spring Cloud —— 简单配置拦截器
- 前后端分离,我怎么就选择了 Spring Boot + Vue 技术栈?
- Spring Batch 作业重启
- Intellij IDEA创建Spring的Hello World项目