首先,要导入jar包:
<!-- SpringBoot 拦截器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
第二可以在启动类加上注解(不需要)
@EnableAspectJAutoProxy(proxyTargetClass = true)
Aspect切面类代码
@Pointcut(value = "execution(public * com.qqri.api.controller..*.*(..))")
public void webLog(){}
@Before(value = "webLog()")
public void doBefore(JoinPoint joinPoint) {
// 接收到请求,打印请求内容
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
// 打印请求内容
log.info("URL : " + request.getRequest