springBoot使用aop添加处理rest请求 打印请求时间 和请求参数
Posted 官萧何
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springBoot使用aop添加处理rest请求 打印请求时间 和请求参数相关的知识,希望对你有一定的参考价值。
贴上aop配置
import com.alibaba.fastjson.JSONObject; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.ArrayList; import java.util.List; @Aspect @Component public class RestAop { /** * // 控制是否开启日志 */ public static Boolean onOff = false; private static Log logger = LogFactory.getLog(RestAop.class); @Pointcut("execution(public * com.mybatis.*.web..*.*(..))") public void pointCutRestDef() { } @Around("pointCutRestDef()") public Object processRst(ProceedingJoinPoint point) throws Throwable { Object returnValue = null; final List<Object> params = new ArrayList<>(); ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = sra.getRequest(); Object[] args = point.getArgs(); for (int i = 0; i < args.length; i++) { Object object = args[i]; if (object instanceof HttpServletResponse) { continue; } if (object instanceof HttpServletRequest) { continue; } params.add(object); } logger.info( "rest method: " + point.getSignature().getDeclaringTypeName() + "." + point.getSignature().getName()); String cloneParams = JSONObject.toJSONString(params); logger.info("rest param: " + cloneParams); Long startTime = System.currentTimeMillis(); try { returnValue = point.proceed(point.getArgs()); } catch (Exception e) { // 请求异常处理 throw e; } Long endTime = System.currentTimeMillis(); logger.info("rest " + request.getRequestURI() + "---used time---" + (endTime - startTime)); Boolean boolean1 = true; if (returnValue != null && !returnValue.equals(boolean1)) { System.out.println(JSONObject.toJSONString(returnValue)); } return returnValue; } }
以上是关于springBoot使用aop添加处理rest请求 打印请求时间 和请求参数的主要内容,如果未能解决你的问题,请参考以下文章
SPRINGBOOT9--AOP的使用(本例展示统一处理Web请求日志)