SpringBoot AOP切面接口开发之参数解析

方法一:

注解类中定义参数名称:

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface DBChange {
​
    String projectId() default "";
​
    String environmentType() default "";
    /**
     *传递参数的对象类型
     */
    Class<?> parameter() default Object.class;
    /**
     * 默认不校验参数
     * @return
     */
    boolean require() default false;
}

切面类中解析参数:

@Aspect
@Component
@Log4j
public class DBChangeAop {
 
    //切面链接处
    @Pointcut("@annotation(com.ggdo.manager.util.DBChange)")
    public void cutMethod(){}
​
    @Before(value = "cutMethod() && @annotation(dbChange)")
    public void doBeforeCut(JoinPoint joinPoint,DBChange dbChange) throws Exception{
        log.info("Before增强");
        
        //自定义工具类解析注接类传入参数
        String projectId = (String)DBChangeResolver.newInstance().resolver(joinPoint,dbChange.projectId());
        String environmentType = (String)DBChangeResolver.newInstance().resolver(joinPoint,dbChange.environmentType());
        log.info("projectId:"+projectId);
        log.info("environmentType:"+environmentType);
        if (projectId == null || "".equals(projectId) ){
            throw new CommonException("projectId不能为空!");
        }
​
    }
​
    @After(value = "cutMethod()")
    public void doAfterCut(JoinPoint joinPoint){
       log.info("After增强");
    }
}

自定义工具类,对注解传入的参数进行解析

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
​
import java.lang.reflect.Method;
​
public class DBChangeResolver {
    private static DBChangeResolver resolver ;
​
    public static DBChangeResolver newInstance(){
​
        if (resolver == null) {
            return resolver = new DBChangeResolver();
        }else{
            return resolver;
        }
​
    }
​
    /**
     * 解析注解上的值
     * @param joinPoint
     * @param str 需要解析的字符串
     * @return
     */
    public Object resolver(JoinPoint joinPoint, String str) {
​
        if (str == null) return null ;
​
        Object value = null;
        if (str.matches("#\\{\\D*\\}")) {// 如果name匹配上了#{},则把内容当作变量
            String newStr = str.replaceAll("#\\{", "").replaceAll("\\}", "");
            if (newStr.contains(".")) { // 复杂类型
                try {
                    value = complexResolver(joinPoint, newStr);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                value = simpleResolver(joinPoint, newStr);
            }
        } else { //非变量
            value = str;
        }
        return value;
    }
​
​
    private Object complexResolver(JoinPoint joinPoint, String str) throws Exception {
​
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
​
        String[] names = methodSignature.getParameterNames();
        Object[] args = joinPoint.getArgs();
        String[] strs = str.split("\\.");
​
        for (int i = 0; i < names.length; i++) {
            if (strs[0].equals(names[i])) {
                Object obj = args[i];
                Method dmethod = obj.getClass().getDeclaredMethod(getMethodName(strs[1]), null);
                Object value = dmethod.invoke(args[i]);
                return getValue(value, 1, strs);
            }
        }
​
        return null;
​
    }
​
    private Object getValue(Object obj, int index, String[] strs) {
​
        try {
            if (obj != null && index < strs.length - 1) {
                Method method = obj.getClass().getDeclaredMethod(getMethodName(strs[index + 1]), null);
                obj = method.invoke(obj);
                getValue(obj, index + 1, strs);
            }
​
            return obj;
​
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
​
    private String getMethodName(String name) {
        return "get" + name.replaceFirst(name.substring(0, 1), name.substring(0, 1).toUpperCase());
    }
​
​
    private Object simpleResolver(JoinPoint joinPoint, String str) {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        String[] names = methodSignature.getParameterNames();
        Object[] args = joinPoint.getArgs();
​
        for (int i = 0; i < names.length; i++) {
            if (str.equals(names[i])) {
                return args[i];
            }
        }
        return null;
    }
​
}

方法添加注解时设置传参:

@PostMapping("/change")
    @DBChange(projectId = "#{resultInfo.projectId}",environmentType = "#{resultInfo.environment}" ,require = true)
    public ResponseObj changeDb(@RequestBody EdcReleaseLogDto resultInfo){
       log.info("测试");
    }

此方法存在问题:如果方法的接收参数的EdcReleaseLogDto中不包含AOP注解需要的参数,但EdcReleaseLogDto的父类DTO中有AOP注解需要的参数,此时AOP不能拿到该参数。

方法二:

考虑到方法一中问题,直接从请求参数中获取AOP注解需要的参数。此方法支持从请求头获取参数或者从请求方法中获取参数,从请求方法中获取参数时,要求入参接收对象本身或者父类有AOP注解需要的对应参数。

@Before(value = "cutMethod()")
    public void doBeforeCut(JoinPoint joinPoint) {
        log.info("请求开始 : ==========================");
        // 接收到请求,记录请求内容
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        log.info("解析请求头参数 : ============开始==========");
        Enumeration<String> headerNames = request.getHeaderNames();
        EDCBaseRequestBody body = new EDCBaseRequestBody();
        while (headerNames.hasMoreElements()) {
            String headName = headerNames.nextElement();
//            log.info("headName->" + headName);
            if ("projectId".equals(headName) || "projectid".equals(headName)) {
                String header = request.getHeader(headName);
                body.setProjectId(Integer.parseInt(header));
                log.info("headName->" + headName + ";header:" + header);
            }
            if ("environment".equals(headName)) {
                String header = request.getHeader(headName);
                body.setEnvironment(header);
                log.info("headName->" + headName + ";header:" + header);
            }
        }
        if (!(StringUtils.isEmpty(body.getEnvironment())) && body.getProjectId() != null) {
            return body;
        }
        log.info("解析请求参数 : =================开始==================");
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        // 只记录post方法 传json格式的数据
        if ("POST".equals(request.getMethod())) {
            LocalVariableTableParameterNameDiscoverer u = new LocalVariableTableParameterNameDiscoverer();
            String[] paramNames = u.getParameterNames(method);
            //方法 1 请求的方法参数值 JSON 格式 null不显示
            if (joinPoint.getArgs().length > 0) {
                Object[] args = joinPoint.getArgs();
                for (int i = 0; i < args.length; i++) {
                    //请求参数类型判断过滤,防止JSON转换报错
                    if (args[i] instanceof HttpServletRequest || args[i] instanceof HttpServletResponse || args[i] instanceof MultipartFile) {
                        continue;
                    }
                    String jsonstr = JSON.toJSONString(args[i]);
                    Map<String, Object> map = JSONObject.parseObject(jsonstr);
                    if (map.containsKey("projectId")) {
                        Integer projectId = ((map.get("projectId")).getClass().equals(String.class)) ? Integer.parseInt((String) map.get("projectId")) : (Integer) map.get("projectId");
                        body.setProjectId(projectId);
                    }
                    if (map.containsKey("environment")) {
                        body.setEnvironment((String) map.get("environment"));
                    }
                    log.info("请求参数名称 :" + paramNames[i] + ", 内容 :" + JSON.toJSONString(args[i]));
                }
                return body;
            }
        }
        if ("GET".equals(request.getMethod())) {
            throw new CommonException("GET请求参数解析异常");
        }
        throw new CommonException("请求参数异常");
    }

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值