引入依赖
<!--aop-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
切面类
@Aspect
@Component
@Slf4j
public class HttpAspect {
//你的控制器路径
@Pointcut("execution(* com.*.controller..*.*(..))")
public void logPointCut() {
}
@Before("logPointCut()")
public void doBefore(JoinPoint joinPoint) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
assert attributes != null;
HttpServletRequest request = attributes.getRequest();
log.info("========================================== Start ==========================================");
log.info("=>> 请求地址 : " + request.getRequestURL().toString());
log.info("=>> 请求方式 : " + request.getMethod());
log.info("=>> 方法名称 : " + joinPoint.getSignature().getDeclaringTypeName() + "."
+ joinPoint.getSignature().getName());
log.info("=>> 参数 : " + Arrays.toString(joinPoint.getArgs()));
}
@AfterReturning(returning = "ret", pointcut = "logPointCut()")
public void doAfterReturning(Object ret) {
log.debug("返回值 : " + ret);
}
@Around("logPointCut()")
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
long startTime = System.currentTimeMillis();
Object ob = pjp.proceed();
log.info("=>> 耗时 : " + (System.currentTimeMillis() - startTime));
log.info("=========================================== End ===========================================");
return ob;
}
}
效果图
可以根据实际情况制定参数的样式等。