AOP + 注解 实现通用的接口参数校验

时间:2023-02-17 11:21:30

大家好,我是小悟

写移动端接口的时候,为了校验参数,传统的做法是加各种判断,写了很多重复的代码,而且也不美观。为了增加代码复用性,美观的校验参数,采用AOP + 注解的方式来实现接口的参数校验(使用拦截器也可以实现),在需要校验参数的方法上加上自定义的注解即可。

代码文件目录

AOP + 注解 实现通用的接口参数校验

代码实现

自定义异常:RRException

public class RRException extends RuntimeException {
private static final long serialVersionUID = 1L;

private String msg;
private int code = 500;

public RRException(String msg) {
super(msg);
this.msg = msg;
}

public RRException(String msg, Throwable e) {
super(msg, e);
this.msg = msg;
}

public RRException(String msg, int code) {
super(msg);
this.msg = msg;
this.code = code;
}

public RRException(String msg, int code, Throwable e) {
super(msg, e);
this.msg = msg;
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
}

全局异常处理器:RRExceptionHandler

@RestControllerAdvice
public class RRExceptionHandler {
private Logger logger = LoggerFactory.getLogger(getClass());
/**
* 处理自定义异常
*/
@ExceptionHandler(RRException.class)
public R handleRRException(RRException e){
R r = new R();
r.put("code", e.getCode());
r.put("msg", e.getMessage());
return r;
}
}

响应数据封装:R

public class R extends HashMap<String, Object> {
private static final long serialVersionUID = 1L;

public R() {
put("code", 0);
put("msg", "success");
}

public static R error() {
return error(500, "未知异常,请联系管理员");
}

public static R error(String msg) {
return error(500, msg);
}
public static R ok(int code, String msg) {
R r = new R();
r.put("code", code);
r.put("msg", msg);
return r;
}
public static R error(int code, String msg) {
R r = new R();
r.put("code", code);
r.put("msg", msg);
return r;
}
public static R ok(String msg) {
R r = new R();
r.put("msg", msg);
return r;
}

public static R ok(Map<String, Object> map) {
R r = new R();
r.putAll(map);
return r;
}

public static R ok(List<Object> list) {
R r = new R();
r.put("msg", list);
return r;
}

public static R ok() {
return new R();
}
@Override
public R put(String key, Object value) {
super.put(key, value);
return this;
}
}

注解:ParamCheck

@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RUNTIME)
public @interface ParamCheck {
//字段校验规则,格式:字段名+校验规则+冒号+错误信息,例如:name<11:名字必须少于11位
String[] value();
}

工具类:ReflectionUtil

public class ReflectionUtil {
private static final String SETTER_PREFIX = "set";
private static final String GETTER_PREFIX = "get";
private static final String CGLIB_CLASS_SEPARATOR = "$$";
private static Logger logger = LoggerFactory.getLogger(ReflectionUtil.class);
/**
* 调用Getter方法.
*/
public static Object invokeGetter(Object obj, String propertyName) {
String getterMethodName = GETTER_PREFIX
+ StringUtils.capitalize(propertyName);
return invokeMethod(obj, getterMethodName, new Class[]{},
new Object[]{});
}
/**
* 直接调用对象方法, 无视private/protected修饰符.
* 用于一次性调用的情况,否则应使用getAccessibleMethod()函数获得Method后反复调用. 同时匹配方法名+参数类型,
*/
public static Object invokeMethod(final Object obj,
final String methodName, final Class<?>[] parameterTypes,
final Object[] args) {
Method method = getAccessibleMethod(obj, methodName, parameterTypes);
if (method == null) {
throw new IllegalArgumentException("Could not find method ["
+ methodName + "] on target [" + obj + "]");
}
try {
return method.invoke(obj, args);
} catch (Exception e) {
throw convertReflectionExceptionToUnchecked(e);
}
}
/**
* 循环向上转型, 获取对象的DeclaredMethod,并强制设置为可访问. 如向上转型到Object仍无法找到, 返回null.
* 匹配函数名+参数类型。
* <p>
* 用于方法需要被多次调用的情况. 先使用本函数先取得Method,然后调用Method.invoke(Object obj, Object...
* args)
*/
public static Method getAccessibleMethod(final Object obj,
final String methodName, final Class<?>... parameterTypes) {
Validate.notNull(obj, "object can't be null");
Validate.notBlank(methodName, "methodName can't be blank");
for (Class<?> searchType = obj.getClass(); searchType != Object.class; searchType = searchType
.getSuperclass()) {
try {
Method method = searchType.getDeclaredMethod(methodName,
parameterTypes);
makeAccessible(method);
return method;
} catch (NoSuchMethodException e) {
// Method不在当前类定义,继续向上转型
}
}
return null;
}
/**
* 改变private/protected的方法为public,尽量不调用实际改动的语句,避免JDK的SecurityManager抱怨。
*/
public static void makeAccessible(Method method) {
if ((!Modifier.isPublic(method.getModifiers()) || !Modifier
.isPublic(method.getDeclaringClass().getModifiers()))
&& !method.isAccessible()) {
method.setAccessible(true);
}
}
/**
* 将反射时的checked exception转换为unchecked exception.
*/
public static RuntimeException convertReflectionExceptionToUnchecked(
Exception e) {
if ((e instanceof IllegalAccessException)
|| (e instanceof IllegalArgumentException)
|| (e instanceof NoSuchMethodException)) {
return new IllegalArgumentException(e);
} else if (e instanceof InvocationTargetException) {
return new RuntimeException(
((InvocationTargetException) e).getTargetException());
} else if (e instanceof RuntimeException) {
return (RuntimeException) e;
}
return new RuntimeException("Unexpected Checked Exception.", e);
}
}

对不同类型的值进行校验:DiffTypeParamCheck

*/
* @description 对不同类型的值进行校验
*/
public class DiffTypeParamCheck {
/**
* 是否不为空
* @param value 字段值
* @param operatorNum 操作数,这里不需要,只是为了参数统一
* @return 是否不为空
*/
public static Boolean isNotNull(Object value, String operatorNum) {
Boolean isNotNull = Boolean.TRUE;
Boolean isStringNull = (value instanceof String) && StringUtils.isEmpty((String) value);
Boolean isCollectionNull = (value instanceof Collection) && CollectionUtils.isEmpty((Collection) value);
if (value == null) {
isNotNull = Boolean.FALSE;
} else if (isStringNull || isCollectionNull) {
isNotNull = Boolean.FALSE;
}
return isNotNull;
}
/**
* 是否大于
* @param value 字段值
* @param operatorNum 操作数
* @return 是否大于
*/
public static Boolean isGreaterThan(Object value, String operatorNum) {
Boolean isGreaterThan = Boolean.FALSE;
if (value == null) {
return Boolean.FALSE;
}
Boolean isStringGreaterThen = (value instanceof String) && ((String) value).length() > Integer.valueOf(operatorNum);
Boolean isLongGreaterThen = (value instanceof Long) && ((Long) value) > Long.valueOf(operatorNum);
Boolean isIntegerGreaterThen = (value instanceof Integer) && ((Integer) value) > Integer.valueOf(operatorNum);
Boolean isShortGreaterThen = (value instanceof Short) && ((Short) value) > Short.valueOf(operatorNum);
Boolean isFloatGreaterThen = (value instanceof Float) && ((Float) value) > Float.valueOf(operatorNum);
Boolean isDoubleGreaterThen = (value instanceof Double) && ((Double) value) > Double.valueOf(operatorNum);
Boolean isCollectionGreaterThen = (value instanceof Collection) && ((Collection) value).size() > Integer.valueOf(operatorNum);
if (isStringGreaterThen || isLongGreaterThen || isIntegerGreaterThen ||
isShortGreaterThen || isFloatGreaterThen || isDoubleGreaterThen || isCollectionGreaterThen) {
isGreaterThan = Boolean.TRUE;
}
return isGreaterThan;
}
/**
* 是否大于等于
* @param value 字段值
* @param operatorNum 操作数
* @return 是否大于等于
*/
public static Boolean isGreaterThanEqual(Object value, String operatorNum) {
Boolean isGreaterThanEqual = Boolean.FALSE;
if (value == null) {
return Boolean.FALSE;
}
Boolean isStringGreaterThenEqual = (value instanceof String) && ((String) value).length() >= Integer.valueOf(operatorNum);
Boolean isLongGreaterThenEqual = (value instanceof Long) && ((Long) value) >= Long.valueOf(operatorNum);
Boolean isIntegerGreaterThenEqual = (value instanceof Integer) && ((Integer) value) >= Integer.valueOf(operatorNum);
Boolean isShortGreaterThenEqual = (value instanceof Short) && ((Short) value) >= Short.valueOf(operatorNum);
Boolean isFloatGreaterThenEqual = (value instanceof Float) && ((Float) value) >= Float.valueOf(operatorNum);
Boolean isDoubleGreaterThenEqual = (value instanceof Double) && ((Double) value) >= Double.valueOf(operatorNum);
Boolean isCollectionGreaterThenEqual = (value instanceof Collection) && ((Collection) value).size() >= Integer.valueOf(operatorNum);
if (isStringGreaterThenEqual || isLongGreaterThenEqual || isIntegerGreaterThenEqual ||
isShortGreaterThenEqual || isFloatGreaterThenEqual || isDoubleGreaterThenEqual || isCollectionGreaterThenEqual) {
isGreaterThanEqual = Boolean.TRUE;
}
return isGreaterThanEqual;
}
/**
* 是否少于
* @param value 字段值
* @param operatorNum 操作数
* @return 是否少于
*/
public static Boolean isLessThan(Object value, String operatorNum) {
Boolean isLessThan = Boolean.FALSE;
if (value == null) {
return Boolean.FALSE;
}
Boolean isStringLessThen = (value instanceof String) && ((String) value).length() < Integer.valueOf(operatorNum);
Boolean isLongLessThen = (value instanceof Long) && ((Long) value) < Long.valueOf(operatorNum);
Boolean isIntegerLessThen = (value instanceof Integer) && ((Integer) value) < Integer.valueOf(operatorNum);
Boolean isShortLessThen = (value instanceof Short) && ((Short) value) < Short.valueOf(operatorNum);
Boolean isFloatLessThen = (value instanceof Float) && ((Float) value) < Float.valueOf(operatorNum);
Boolean isDoubleLessThen = (value instanceof Double) && ((Double) value) < Double.valueOf(operatorNum);
Boolean isCollectionLessThen = (value instanceof Collection) && ((Collection) value).size() < Integer.valueOf(operatorNum);
if (isStringLessThen || isLongLessThen || isIntegerLessThen ||
isShortLessThen || isFloatLessThen || isDoubleLessThen || isCollectionLessThen) {
isLessThan = Boolean.TRUE;
}
return isLessThan;
}
/**
* 是否少于等于
* @param value 字段值
* @param operatorNum 操作数
* @return 是否少于等于
*/
public static Boolean isLessThanEqual(Object value, String operatorNum) {
Boolean isLessThanEqual = Boolean.FALSE;
if (value == null) {
return Boolean.FALSE;
}
Boolean isStringLessThenEqual = (value instanceof String) && ((String) value).length() <= Integer.valueOf(operatorNum);
Boolean isLongLessThenEqual = (value instanceof Long) && ((Long) value) <= Long.valueOf(operatorNum);
Boolean isIntegerLessThenEqual = (value instanceof Integer) && ((Integer) value) <= Integer.valueOf(operatorNum);
Boolean isShortLessThenEqual = (value instanceof Short) && ((Short) value) <= Short.valueOf(operatorNum);
Boolean isFloatLessThenEqual = (value instanceof Float) && ((Float) value) <= Float.valueOf(operatorNum);
Boolean isDoubleLessThenEqual = (value instanceof Double) && ((Double) value) <= Double.valueOf(operatorNum);
Boolean isCollectionLessThenEqual = (value instanceof Collection) && ((Collection) value).size() <= Integer.valueOf(operatorNum);
if (isStringLessThenEqual || isLongLessThenEqual || isIntegerLessThenEqual ||
isShortLessThenEqual || isFloatLessThenEqual || isDoubleLessThenEqual || isCollectionLessThenEqual) {
isLessThanEqual = Boolean.TRUE;
}
return isLessThanEqual;
}
/**
* 是否不等于
* @param value 字段值
* @param operatorNum 操作数
* @return 是否不等于
*/
public static Boolean isNotEqual(Object value, String operatorNum) {
Boolean isNotEqual = Boolean.FALSE;
if (value == null) {
return Boolean.FALSE;
}
Boolean isStringNotEqual = (value instanceof String) && !value.equals(operatorNum);
Boolean isLongNotEqual = (value instanceof Long) && !value.equals(Long.valueOf(operatorNum));
Boolean isIntegerNotEqual = (value instanceof Integer) && !value.equals(Integer.valueOf(operatorNum));
Boolean isShortNotEqual = (value instanceof Short) && !value.equals(Short.valueOf(operatorNum));
Boolean isFloatNotEqual = (value instanceof Float) && !value.equals(Float.valueOf(operatorNum));
Boolean isDoubleNotEqual = (value instanceof Double) && !value.equals(Double.valueOf(operatorNum));
Boolean isCollectionNotEqual = (value instanceof Collection) && ((Collection) value).size() != Integer.valueOf(operatorNum);
if (isStringNotEqual || isLongNotEqual || isIntegerNotEqual ||
isShortNotEqual || isFloatNotEqual || isDoubleNotEqual || isCollectionNotEqual) {
isNotEqual = Boolean.TRUE;
}
return isNotEqual;
}

切面类:ParamCheckAspect

@Aspect
@Component
public class ParamCheckAspect {
private static final Logger logger = LoggerFactory.getLogger(ParamCheckAspect.class);
@Autowired
private CheckUtil checkUtil;
@Around(value = "@annotation(com.smartMap.media.common.paramcheck.annotation.ParamCheck)")
public Object check(ProceedingJoinPoint point) throws Throwable {
Object obj;
// 参数校验
String msg = checkUtil.doCheck(point);
if (!StringUtils.isEmpty(msg)) {
throw new RRException(msg, 400);
}
// 通过校验,继续执行原有方法
obj = point.proceed();
return obj;
}
}

测试验证

参数实体类:SelectorObj

@Data
public class SelectorObj {
private String value;
private String label;
}

控制器:TestController

@RestController
@RequestMapping("/mobile/test")
public class TestController {
@ParamCheck({"value:value 不能为空","label!=123:label 不能为123"})
@RequestMapping("testParamCheck")
public R testParamCheck(SelectorObj obj) {
System.out.println(obj);
return R.ok().put("obj",obj);
}
}


结果:

1、非空检验

AOP + 注解 实现通用的接口参数校验

AOP + 注解 实现通用的接口参数校验

2、非特定值校验

AOP + 注解 实现通用的接口参数校验

AOP + 注解 实现通用的接口参数校验

您的一键三连,是我更新的最大动力,谢谢

山水有相逢,来日皆可期,谢谢阅读,我们再会

我手中的金箍棒,上能通天,下能探海