背景:在业务中要写许多if判断条件,抛出异常。业务代码与断言代码耦合,可读性极差,即使引入@Validated进行分组校验,也存在大量需要自己断言的地方。
oauth2认证过程中要根据不同模式校验参数,代码已经优化处理,只对入参判空,还有client_id、client_secret、username、password、redirect_uri、code验证
解决方案:断言工具类
package com.erss.common.core.utils;
import com.erss.common.core.domain.R;
import com.erss.common.core.exception.base.BaseException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import java.util.Map;
/**
* 断言工具类
*
* @author weimingzhong
* @date 2022/10/24 18:00
*/
public class AssertUtil {
private static final Logger log = LoggerFactory.getLogger(AssertUtil.class);
/**
* 判断字符串是否为空
*
* AssertUtil.strIsEmpty(accessTokenBody.getCode(), "授权码为空");
*
* @param str 字符串
* @param returnMsg 返回消息
* @author weimingzhong
* @date 2022/11/25 17:26
*/
public static void strIsEmpty(String str, String returnMsg) {
if (StringUtils.isEmpty(str)) {
throw new BaseException(returnMsg);
}
}
/**
* 判断字符串是否为空
*
* AssertUtil.strIsEmpty(OAUTH2_MODULE, OAUTH2_ERROR_D0103.getCode(), null, accessTokenBody.getCode(), "授权码为空", OAUTH2_ERROR_D0103.getDesc());
*
* @param str 字符串
* @param returnMsg 返回消息
* @author weimingzhong
* @date 2022/11/25 17:26
*/
public static void strIsEmpty(String module, String code, Object[] args, String str, String realMsg, String returnMsg) {
if (StringUtils.isEmpty(str)) {
if (StringUtils.isNoneEmpty(realMsg)) {
log.info(realMsg);
}
throw new BaseException(module, code, args, returnMsg);
}
}
/**
* 判断字符串是否相等
*
* @param s1 字符串1
* @param s2 字符串2
* @param returnMsg 返回消息
* @author weimingzhong
* @date 2022/11/25 17:29
*/
public static void strIsNotEquals(String s1, String s2, String returnMsg) {
if (!StringUtils.equals(s1, s2)) {
throw new BaseException(returnMsg);
}
}
/**
* 判断Long类型是否相等
*
* @param l1 参数一
* @param l2 阐述二
* @param returnMsg 返回消息
* @author weimingzhong
* @date 2022/11/25 17:29
*/
public static void longIsEquals(Long l1, Long l2, String returnMsg) {
if (l1.equals(l2)) {
throw new BaseException(returnMsg);
}
}
/**
* @param obj 对象
* @param returnMsg 返回消息
* @author weimingzhong
* @date 2022/11/25 17:31
*/
public static void objIsNotNull(Object obj, String returnMsg) {
if (ObjectUtils.isEmpty(obj)) {
throw new BaseException(returnMsg);
}
}
/**
* @param b 布尔值
* @param returnMsg 返回消息
* @author weimingzhong
* @date 2022/11/25 17:25
*/
public static void booleanIsTrue(Boolean b, String returnMsg) {
if (!b) {
throw new BaseException(returnMsg);
}
}
/**
* 如果参数1大于参数2抛出异常,
* AssertUtil.integerCompare(parentNode.getLevel(), START_LEVEL, "最大层级为两层");
*
* @param int1 参数1
* @param int2 参数2
* @param returnMsg 返回消息
* @author weimingzhong
* @date 2022/11/11 11:22
*/
public static void integerCompare(Integer int1, Integer int2, String returnMsg) {
if (int1 > int2) {
throw new BaseException(returnMsg);
}
}
/**
* 用于查询数据存在
* AssertUtil.checkDataBaseExist(manageNavigationMapper.checkHaveChildren(id), "请先删除子节点");
*
* @param result 数据库结果
* @param returnMsg 返回消息
* @author weimingzhong
* @date 2022/11/11 11:22
*/
public static void checkDataBaseExist(int result, String returnMsg) {
if (result > 0) {
throw new BaseException(returnMsg);
}
}
/**
* 用于插入数据
*
* AssertUtil.insertDataBaseIsOk(expertInfoMapper.insertExpertInfo(expertInfo),"添加数据失败");
*
* @param result 数据库结果
* @param returnMsg 返回消息
* @author weimingzhong
* @date 2022/11/11 11:22
*/
public static void dataBaseActionIsOk(int result, String returnMsg) {
if (result == 0) {
throw new BaseException(returnMsg);
}
}
/**
*
*
* @param r 内部服务请求
* @return 结果
* @author weimingzhong
* @date 2022/12/6 18:01
*/
public static void innerServerIsOk(R r) {
if (R.FAIL == r.getCode()) {
throw new BaseException("网络异常");
}
}
}