引入 aop jar
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
启动类配置开启切面 @EnableAspectJAutoProxy
@Aspect 说明当前是切面配置类
@Order(-10) 切面介入的时机,值越小越早运行切面
@Aspect
@Slf4j
@Component
@Order(-10)
public class LogAspect {
// 只要方法上有requestMaping就会打印 请求参数,返回参数,以及响应时间
@Around("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
public Object logAspectAround(ProceedingJoinPoint point) throws Throwable {
Signature signature = point.getSignature();
long startTime = System.currentTimeMillis();
Object proceed = point.proceed();
log.info("{} --->>> {}, request body is : [{}], response body : [{}], interface time consuming : [{}]", signature.getDeclaringTypeName(), signature.getName(), JSONObject.toJSONString(point.getArgs()), JSONObject.toJSONString(proceed), (System.currentTimeMillis() - startTime) / 1000);
return proceed;
}
}