package com.irdstudio.basic.framework.core.annotation.checkAnnotation; import java.lang.annotation.*; /** * 需求编号:1=1校验 * 问题编号:条件查询检查可选条件是否全部为空 * 开发人员:【xieziwei】 * 创建日期:【2021/3/4 17:05】 * 功能描述: */ @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited public @interface QueryParamsNullCheck { /** * 校验对象 * * @return */ String objName() default ""; /** * 需要校验的字段名字符串数组 * * @return */ String[] queryParamNames() default {}; }
package com.irdstudio.basic.framework.core.annotation.checkAnnotation; import com.irdstudio.basic.framework.core.exception.ValidateException; import com.irdstudio.basic.framework.core.util.StringUtil; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.reflect.MethodSignature; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import java.lang.reflect.Method; import java.util.*; /** * @author xieziwei */ @Aspect @Component public class QueryParamNullCheckAspect { private Logger logger = LoggerFactory.getLogger(QueryParamNullCheckAspect.class); @Around("@annotation(com.irdstudio.basic.framework.core.annotation.checkAnnotation.QueryParamsNullCheck)") public Object around(ProceedingJoinPoint joinPoint) throws Throwable { logger.info("查询参数是否空切面校验开始>>>>>>>>>>>>>>>>>>>>>>>>"); MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); //获取函数的参数名 String[] parameterNames = signature.getParameterNames(); //获取redislock注解对象 QueryParamsNullCheck annotation = method.getAnnotation(QueryParamsNullCheck.class); //获取校验对象的参数名 String objName = annotation.objName(); //获取校验字段的字段名 String[] ParamNames = annotation.queryParamNames(); //获取校验对象的参数值(这里只有一个,故取数组的第一个) Object obj = joinPoint.getArgs()[0]; try { // 取对象的所有方法; Method[] methods = obj.getClass().getDeclaredMethods(); Map<String,String> attrMap = new HashMap<>(); if (methods.length > 0){ // 遍历数组,保存 属性名-属性值 到map中 for (Method item : methods){ if (item.getName().startsWith("get")){ Object o = item.invoke(obj); String attrName = item.getName().substring(3); String attrValue = ""; if (Objects.nonNull(o) && StringUtil.isNotEmpty(o.toString())){ attrValue = o.toString(); } attrMap.put(attrName,attrValue); } } // 遍历map 比较不等参数后的属性是否为空 if (attrMap.size() > 0){ for (String col : ParamNames){ for (Map.Entry<String,String> entry : attrMap.entrySet()){ if (entry.getKey().equalsIgnoreCase(col)){ if (StringUtil.isNotEmpty(entry.getValue())){ logger.info("查询参数是否空切面校验通过>>>>>>>>>>>>>>>>>>>>>>>>"); try { return joinPoint.proceed(); } catch (Throwable throwable) { throwable.printStackTrace(); } } } } } logger.info("查询参数是否全空切面校验不通过>>>>>>>>>>>>>>>>>>>>>>>>"); throw new Throwable(); } } } catch (Exception e) { e.printStackTrace(); } logger.info("查询参数是否全空切面校验不通过>>>>>>>>>>>>>>>>>>>>>>>>"); throw new Throwable(); } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!--Spring工具类 --> <bean class="com.irdstudio.basic.framework.core.util.SpringContextUtils" />
<bean id="queryParamNullCheckAspect" class="com.irdstudio.basic.framework.core.annotation.checkAnnotation.QueryParamNullCheckAspect" /> </beans>
@QueryParamsNullCheck(objName = "reconciliationDocumentsVo",queryParamNames = {"prdId","partnerId"})