@Aspect @Configuration public class RequestAspect { public RequestAspect() { } @Pointcut("execution(* *..*Controller.*(..))") public void controllerAspect() { } @Before("controllerAspect()") public void printRequestParam(JoinPoint joinPoint) { HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest(); String className = joinPoint.getTarget().getClass().getName(); String method = joinPoint.getSignature().getName(); if (!this.haveServletRequest(joinPoint.getArgs())) { String methodParam = JsonUtils.toJson(this.initparam(joinPoint)); RequestLog.info("请求入参: " + methodParam + " " + className.substring(className.lastIndexOf(".")) + "." + method); } } @AfterReturning( returning = "ret", pointcut = "controllerAspect()" ) public void printResponseParam(Object ret) { if (ret instanceof String) { if (ret.toString().indexOf("recordsTotal") <= 0) { RequestLog.info("返回参数: " + ret.toString()); } } else { RequestLog.info("返回参数: " + JsonUtils.toJson(ret)); } } private Map<String, Object> initparam(JoinPoint joinPoint) { Signature signature = joinPoint.getSignature(); MethodSignature methodSignature = (MethodSignature)signature; String[] keys = methodSignature.getParameterNames(); Object[] args = joinPoint.getArgs(); Map<String, Object> params = new LinkedHashMap(); for(int i = 0; i < keys.length; ++i) { if (args[i] instanceof InputStreamSource) { params.put(keys[i], "文件类型" + args[i].getClass().getName()); } else { params.put(keys[i], args[i]); } } return params; } private boolean haveServletRequest(Object[] args) { if (args.length <= 0) { return false; } else { for(int i = 0; i < args.length; ++i) { if (i >= 2) { return false; } if (args[i] instanceof ServletRequest) { return true; } } return false; } } }
将每次的请求和相应的参数都打印出来的设计方案-拦截器
猜你喜欢
转载自blog.csdn.net/yz18931904/article/details/130748970
今日推荐
周排行