Spring 断言工具类:优雅地验证方法参数和状态
在开发过程中,为了保证代码的质量和安全性,经常需要在方法中对传入参数或对象状态进行验证和检查,以确保它们符合约定和预期。这时就可以使用
Spring 提供的断言工具类来进行优雅的参数验证,避免繁琐的 if/else 判断,并且可以提高代码的可读性和易维护性。
Spring 的断言工具类主要包括在 org.springframework.util.Assert 和 org.springframework.util.AssertionErrors 两个类中,其中 Assert 提供了一组静态方法,可以对传入参数进行非空、布尔条件等方面的验证,例如:
public void doSomething(String arg1, int arg2) {
Assert.notNull(arg1, "arg1 must not be null");
Assert.isTrue(arg2 > 0 && arg2 < 100, "arg2 must be in range [1, 99]");
// ...
}
上述方法中,Assert.notNull 方法用于验证字符串参数 arg1 不为 null,否则抛出 IllegalArgumentException 异常,并输出指定的错误信息。Assert.isTrue 方法则用于验证整数参数 arg2 在指定的范围内,否则抛出相应的异常。在验证失败时,还可以在异常消息中使用占位符 ${} 来引用参数值,以便更精确地定位错误的原因。
除了 Assert 类,Spring 还提供了断言回调接口 org.springframework.util.Assert#AssertionCallback,以支持更加通用的断言操作,并允许在断言失败时进行相应的操作。例如:
public void doSomething(List<String> list) {
Assert.notEmpty(list, "list must not be empty", () -> {
System.out.println("Empty list handled");
throw new IllegalArgumentException("Empty list not allowed");
});
// ...
}
上述方法中,Assert.notEmpty 方法用于验证列表 list 不为空,否则在断言失败时执行回调函数,打印相应的信息,并抛出 IllegalArgumentException 异常。该方法还支持在回调函数中返回一个结果,以便在需要时进行相应的处理。
除了断言工具类之外,Spring 还提供了很多其他方面的工具类和辅助函数,以提高开发效率并简化代码逻辑。例如:
org.springframework.util.StringUtils 提供了一组字符工具方法,可以进行字符串的快速操作和格式化,例如 StringUtils.trimWhitespace、StringUtils.split 等。
org.springframework.util.CollectionUtils 提供了一组集合工具方法,可以处理集合的合并、过滤、排序等操作,例如 CollectionUtils.union、CollectionUtils.filter 等。
org.springframework.util.NumberUtils 提供了一组数字工具方法,可以进行数字类型的转换和比较,例如 NumberUtils.parseNumber、NumberUtils.compare 等。
断言工具类代码示例:
/**
* 断言工具类
* <p style="background-color: green">程序中使用尽量使用参数中含有异常信息的方法 避免异常信息提示不清晰</p>
* 断言尽量用于service层业务、程序处理、参数校验
* 本类断言的使用分为业务、程序、参数校验 在使用时请分清楚
* @program education-saas-api
* @since 2020-10-07 10:16
**/
@UtilityClass
public class AssertUtil {
/**
* 断言是否为true(抛出业务异常)
*
* @param flag 被断言表达式
* @param msg 异常信息
*/
public void businessIsTrue(boolean flag, String msg) {
if (!flag) {
throw new BusinessException(msg);
}
}
/**
* 断言对象是否不为空(抛出业务异常)
* @param obj 对象
* @param msg 异常信息
*/
public <T> void businessisNull(T obj, String msg) {
if ((Integer) obj >0) {
throw new BusinessException(msg);
}
}
/**
* 断言对象是否不为空(抛出业务异常)
* @param obj 对象
* @param msg 异常信息
*/
public <T> void businessNotNullIsZero(T obj, String msg) {
if ((Integer) obj ==0) {
throw new BusinessException(msg);
}
}
/**
* 断言是否为true(抛出参数校验异常)
*
* @param flag 被断言表达式
* @param msg 异常信息
*/
public void paramCheckIsTrue(boolean flag, String msg) {
if (!flag) {
throw new ParamCheckException(msg);
}
}
/**
* 断言是否为true(抛出业务异常)
*
* @param flag 被断言表达式
*/
public void businessIsTrue(boolean flag) {
businessIsTrue(flag, "业务异常--->该表达式必须为true");
}
/**
* 断言是否为true(抛出程序异常)
*
* @param flag 被断言表达式
* @param msg 异常信息
* @throws ProgramException 抛出异常
*/
public void programIsTrue(boolean flag, String msg) throws ProgramException {
if (!flag) {
throw new ProgramException(msg);
}
}
/**
* 断言是否为true(抛出程序异常)
*
* @param flag 被断言表达式
* @throws ProgramException 抛出异常
*/
public void programIsTrue(boolean flag) throws ProgramException {
programIsTrue(flag, "程序异常--->该表达式必须为true");
}
/**
* 断言对象是否为空(抛出业务异常)
*
* @param obj 对象
* @param msg 异常信息
*/
public void businessIsNull(Object obj, String msg) {
if (Objects.nonNull(obj)) {
throw new BusinessException(msg);
}
}
/**
* 断言对象是否为空(抛出参数校验异常)
*
* @param obj 对象
* @param msg 异常信息
*/
public void paramCheckIsNull(Object obj, String msg) {
if (Objects.nonNull(obj)) {
throw new ParamCheckException(msg);
}
}
/**
* 断言对象是否为空(抛出业务异常)
*
* @param obj 对象
*/
public void businessIsNull(Object obj) {
businessIsNull(obj, "业务异常--->对象不为空");
}
/**
* 断言对象是否为空(抛出程序异常)
*
* @param obj 对象
* @param msg 异常信息
* @throws ProgramException 抛出异常
*/
public void programIsNull(Object obj, String msg) throws ProgramException {
if (Objects.nonNull(obj)) {
throw new ProgramException(msg);
}
}
/**
* 断言对象是否为空(抛出程序异常)
*
* @param obj 对象
* @throws ProgramException 抛出异常
*/
public void programIsNull(Object obj) throws ProgramException {
programIsNull(obj, "程序异常--->对象不为空");
}
/**
* 断言对象是否不为空(抛出业务异常)
*
* @param obj 对象
* @param msg 异常信息
* @return 不为空对象
*/
public <T> T businessNotNull(T obj, String msg) {
if (Objects.isNull(obj)) {
throw new BusinessException(msg);
} else {
return obj;
}
}
/**
* 断言对象是否不为空(抛出业务异常)
* @param obj 对象
* @param msg 异常信息
*/
public <T> void businessNotNullInstall(T obj, String msg) {
if ((Integer) obj <0) {
throw new BusinessException(msg);
}
}
/**
* 断言对象是否不为空(抛出参数校验异常)
*
* @param obj 对象
* @param msg 异常信息
* @return 不为空对象
*/
public <T> T paramCheckNotNull(T obj, String msg) {
if (Objects.isNull(obj) || " ".equals(obj) || "".equals(obj)) {
throw new ParamCheckException(msg);
} else {
return obj;
}
}
/**
* 断言对象是否不为0(抛出参数校验异常)
* @param obj 对象
* @param msg 异常信息
* @return 不为空对象
*/
public <T> void paramNotNullIsZero(T obj, String msg) {
if ((Integer) obj ==0) {
throw new BusinessException(msg);
}
}
/**
* 断言对象是否不为空(抛出业务异常)
*
* @param obj 对象
* @return 不为空对象
*/
public <T> T businessNotNull(T obj) {
return businessNotNull(obj, "业务异常--->对象为空");
}
/**
* 断言对象是否不为空(抛出业务异常)
*
* @param obj 对象
* @return 不为空对象
*/
public <T> T businessNotNullUser(T obj) {
return businessNotNull(obj, "业务异常--->用户名相同");
}
/**
* 断言对象是否不为空(抛出业务异常)
*
* @param obj 对象
* @param msg 异常信息
* @return 不为空对象
* @throws ProgramException 程序异常
*/
public <T> T programNotNull(T obj, String msg) throws ProgramException {
if (Objects.isNull(obj)) {
throw new ProgramException(msg);
} else {
return obj;
}
}
/**
* 断言对象是否不为空(抛出业务异常)
*
* @param obj 对象
* @return 不为空对象
* @throws ProgramException 系统异常
*/
public <T> T programNotNull(T obj) throws ProgramException {
return programNotNull(obj, "程序异常--->对象为空");
}
/**
* 断言字符串是否为空(抛出业务异常)
*
* @param str 字符串
* @param msg 异常信息
* @return 不为空字符串
*/
public <T extends CharSequence> T businessStrNotEmpty(T str, String msg) {
if (StringUtils.isEmpty(str)) {
throw new BusinessException(msg);
} else {
return str;
}
}
/**
* 断言字符串是否为空(抛出参数校验异常)
*
* @param str 字符串
* @param msg 异常信息
* @return 不为空字符串
*/
public <T extends CharSequence> T paramCheckStrNotEmpty(T str, String msg) {
if (StringUtils.isEmpty(str)) {
throw new ParamCheckException(msg);
} else {
return str;
}
}
/**
* 断言字符串是否为空(抛出业务异常)
*
* @param str 字符串
* @return 不为空字符串
*/
public <T extends CharSequence> T businessStrNotEmpty(T str) {
return businessStrNotEmpty(str, "业务异常--->字符串为空");
}
/**
* 断言字符串是否为空(抛出程序异常)
*
* @param str 字符串
* @param msg 异常信息
* @return 不为空字符串
* @throws ProgramException 抛出异常
*/
public <T extends CharSequence> T programStrNotEmpty(T str, String msg) throws ProgramException {
if (StringUtils.isEmpty(str)) {
throw new ProgramException(msg);
} else {
return str;
}
}
/**
* 断言字符串是否为空(抛出业务异常)
*
* @param str 字符串
* @return 不为空字符串
* @throws ProgramException 抛出异常
*/
public <T extends CharSequence> T programStrNotEmpty(T str) throws ProgramException {
return programStrNotEmpty(str, "程序异常--->字符串为空");
}
/**
* 断言字符串是否为空或为全是空格字符串(抛出业务异常)
*
* @param str 字符串
* @param msg 异常信息
* @return 不为空字符串
*/
public <T extends CharSequence> T businessStrNotBlank(T str, String msg) {
if (StringUtils.isBlank(str)) {
throw new BusinessException(msg);
} else {
return str;
}
}
/**
* 断言字符串是否为空或为全是空格字符串(抛出参数校验异常)
*
* @param str 字符串
* @param msg 异常信息
* @return 不为空字符串
*/
public <T extends CharSequence> T paramCheckStrNotBlank(T str, String msg) {
if (StringUtils.isBlank(str)) {
throw new ParamCheckException(msg);
} else {
return str;
}
}
/**
* 断言字符串是否为空或为全是空格字符串(抛出业务异常)
*
* @param str 字符串
* @return 不为空字符串
*/
public <T extends CharSequence> T businessStrNotBlank(T str) {
return businessStrNotBlank(str, "业务异常--->字符串为空");
}
/**
* 断言字符串是否为空或为全是空格字符串(抛出业务异常)
*
* @param str 字符串
* @param msg 异常信息
* @return 不为空字符串
* @throws ProgramException 抛出异常
*/
public <T extends CharSequence> T programStrNotBlank(T str, String msg) throws ProgramException {
if (StringUtils.isBlank(str)) {
throw new ProgramException(msg);
} else {
return str;
}
}
/**
* 断言字符串是否为空或为全是空格字符串(抛出业务异常)
*
* @param str 字符串
* @return 不为空字符串
* @throws ProgramException 抛出异常
*/
public <T extends CharSequence> T programStrNotBlank(T str) throws ProgramException {
return programStrNotBlank(str, "业务异常--->字符串为空");
}
/**
* 断言字符串是否为空(抛出业务异常)
*
* @param str 字符串
* @param msg 异常信息
*/
public <T extends CharSequence> void businessStrIsEmpty(T str, String msg) {
if (StringUtils.isNotEmpty(str)) {
throw new BusinessException(msg);
}
}
/**
* 断言字符串是否为空(抛出参数校验异常)
*
* @param str 字符串
* @param msg 异常信息
*/
public <T extends CharSequence> void paramCheckStrIsEmpty(T str, String msg) {
if (StringUtils.isNotEmpty(str)) {
throw new ParamCheckException(msg);
}
}
/**
* 断言字符串是否为空(抛出业务异常)
*
* @param str 字符串
*/
public <T extends CharSequence> void businessStrIsEmpty(T str) {
businessStrIsEmpty(str, "业务异常--->字符串不为空");
}
/**
* 断言字符串是否为空(抛出程序异常)
*
* @param str 字符串
* @param msg 异常信息
* @throws ProgramException 抛出异常
*/
public <T extends CharSequence> void programStrIsEmpty(T str, String msg) throws ProgramException {
if (StringUtils.isNotEmpty(str)) {
throw new ProgramException(msg);
}
}
/**
* 断言字符串是否为空(抛出业务异常)
*
* @param str 字符串
* @throws ProgramException 抛出异常
*/
public <T extends CharSequence> void programStrIsEmpty(T str) throws ProgramException {
programStrIsEmpty(str, "程序异常--->字符串不为空");
}
/**
* 断言字符串是否为空或为全是空格字符串(抛出业务异常)
*
* @param str 字符串
* @param msg 异常信息
*/
public <T extends CharSequence> void businessStrIsBlank(T str, String msg) {
if (StringUtils.isNotBlank(str)) {
throw new BusinessException(msg);
}
}
/**
* 断言字符串是否为空或为全是空格字符串(抛出参数校验异常)
*
* @param str 字符串
* @param msg 异常信息
*/
public <T extends CharSequence> void paramCheckStrIsBlank(T str, String msg) {
if (StringUtils.isNotBlank(str)) {
throw new ParamCheckException(msg);
}
}
/**
* 断言字符串是否为空或为全是空格字符串(抛出业务异常)
*
* @param str 字符串
*/
public <T extends CharSequence> void businessStrIsBlank(T str) {
businessStrIsBlank(str, "业务异常--->字符串不为空");
}
/**
* 断言字符串是否为空或为全是空格字符串(抛出业务异常)
*
* @param str 字符串
* @param msg 异常信息
* @throws ProgramException 抛出异常
*/
public <T extends CharSequence> void programStrIsBlank(T str, String msg) throws ProgramException {
if (StringUtils.isNotBlank(str)) {
throw new ProgramException(msg);
}
}
/**
* 断言字符串是否为空或为全是空格字符串(抛出业务异常)
*
* @param str 字符串
* @throws ProgramException 抛出异常
*/
public <T extends CharSequence> void programStrIsBlank(T str) throws ProgramException {
programStrIsBlank(str, "业务异常--->字符串不为空");
}
/**
* 断言collection是否为null或是否为空集合(抛出业务异常)
*
* @param collection 被断言collection
* @param msg 异常信息
* @return 不为空collection
*/
public <T> Collection<T> businessCollectionNotEmpty(Collection<T> collection, String msg) {
if (Objects.isNull(collection) || collection.isEmpty()) {
throw new BusinessException(msg);
} else {
return collection;
}
}
/**
* 断言collection是否为null或是否为空集合(抛出参数校验异常)
*
* @param collection 被断言collection
* @param msg 异常信息
* @return 不为空collection
*/
public <T> Collection<T> paramCheckCollectionNotEmpty(Collection<T> collection, String msg) {
if (Objects.isNull(collection) || collection.isEmpty()) {
throw new ParamCheckException(msg);
} else {
return collection;
}
}
/**
* 断言collection是否为null或是否为空集合(抛出业务异常)
*
* @param collection 被断言collection
* @return 不为空collection
*/
public <T> Collection<T> businessCollectionNotEmpty(Collection<T> collection) {
return businessCollectionNotEmpty(collection, "业务异常--->collection为空");
}
/**
* 断言collection是否为null或是否为空集合(抛程序务异常)
*
* @param collection 被断言collection
* @param msg 异常信息
* @return 不为空collection
* @throws ProgramException 抛出异常
*/
public <T> Collection<T> programCollectionNotEmpty(Collection<T> collection, String msg) throws ProgramException {
if (Objects.isNull(collection) || collection.isEmpty()) {
throw new BusinessException(msg);
} else {
return collection;
}
}
/**
* 断言collection是否为null或是否为空集合(抛程序务异常)
*
* @param collection 被断言collection
* @return 不为空collection
* @throws ProgramException 抛出异常
*/
public <T> Collection<T> programCollectionNotEmpty(Collection<T> collection) throws ProgramException {
return programCollectionNotEmpty(collection, "程序异常--->collection为空");
}
/**
* 断言map是否为null或为空map(抛出业务异常)
*
* @param map 被断言map
* @param msg 异常信息
* @return 不为空map
*/
public <K, V> Map<K, V> businessMapNotEmpty(Map<K, V> map, String msg) {
if (Objects.isNull(map) || map.isEmpty()) {
throw new BusinessException(msg);
} else {
return map;
}
}
/**
* 断言map是否为null或为空map(抛出业务异常)
*
* @param map 被断言map
* @return 不为空map
*/
public <K, V> Map<K, V> businessMapNotEmpty(Map<K, V> map) {
return businessMapNotEmpty(map, "业务异常--->map为空");
}
/**
* 断言map是否为null或为空map(抛出业务异常)
*
* @param map 被断言map
* @param msg 异常信息
* @return 不为空map
* @throws ProgramException 抛出异常
*/
public <K, V> Map<K, V> programMapNotEmpty(Map<K, V> map, String msg) throws ProgramException {
if (Objects.isNull(map) || map.isEmpty()) {
throw new ProgramException(msg);
} else {
return map;
}
}
/**
* 断言map是否为null或为空map(抛出业务异常)
*
* @param map 被断言map
* @return 不为空map
* @throws ProgramException 抛出异常
*/
public <K, V> Map<K, V> programMapNotEmpty(Map<K, V> map) throws ProgramException {
return programMapNotEmpty(map, "程序异常--->map为空");
}
}
总之,Spring 提供的断言工具类和其他辅助工具类可以大大简化开发过程中的参数验证和状态检查,同时提高代码的可读性和可维护性,为开发者带来便利和效率的提升。
相关文章
- Spring MVC程序中得到静态资源文件css,js,图片
- Spring容器关闭前执行销毁方法的几种方式
- 使用JDBCTemplate实现与Spring结合,方法公用 ——接口(BaseDao)
- SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
- 当spring 容器初始化完成后执行某个方法 防止onApplicationEvent方法被执行两次
- Spring和Hibernate集成的HibernateTemplate的一些常用方法总结
- spring boot: 中文显示乱码,在applicationContext里面配置
- Spring Cloud Gateway 突发高危漏洞,下一代云原生网关恰逢其时?
- Spring Boot 如何在启动后执行初始化任务(转)
- spring boot:thymeleaf给fragment传递参数的方法(spring boot 2.3.3)
- Spring boot配置文件application.properties和bootstrap.properties的区别
- 【Java】MyBatis与Spring框架整合(二)
- Spring和Struts2配置文件不在默认路径下的web.xml配置方法
- Spring配置文件总结
- spring boot:thymeleaf给fragment传递参数的方法(spring boot 2.3.3)
- Spring Cloud Alibaba与Spring Boot、Spring Cloud之间不得不说的版本关系
- spring事务--使用aop事务代理对象调用方法示例
- Spring 自定义注解使用案例 首先创建一个注解@interface
- Spring管理的bean初始化方法的三种方式,以及@PostConstruct不起作用的原因
- Spring概述
- spring security 在controller层 方法级别使用注解 @PreAuthorize("hasRole('ROLE_xxx')")设置权限拦截 ,无权限则返回403
- Spring的DataSource配置、将Hibernate配置所有写到Spring配置
- Spring MVC参数方法名称解析器
- Spring Cloud Alibaba微服务第14章之分库分表