SpringBoot AOP @Aspect注解 打印访问请求和返回数据

 1 package com.hangtian.admin.framework.security.filter;
 2 
 3 import com.google.gson.Gson;
 4 import com.hangtian.common.utils.StringUtils;
 5 import lombok.extern.slf4j.Slf4j;
 6 import org.aspectj.lang.JoinPoint;
 7 import org.aspectj.lang.annotation.AfterReturning;
 8 import org.aspectj.lang.annotation.Aspect;
 9 import org.aspectj.lang.annotation.Before;
10 import org.aspectj.lang.annotation.Pointcut;
11 import org.aspectj.lang.reflect.MethodSignature;
12 import org.springframework.stereotype.Component;
13 
14 import java.lang.reflect.Method;
15 
16 @Aspect
17 @Component
18 @Slf4j
19 public class LogFilter {
20 
21 
22     // 统一切点,对com.hangtian.admin.controller及其子包中所有的类的所有方法切面
23     @Pointcut("execution(public * com.hangtian.admin.controller..*.*(..))")
24     public void Pointcut() {
25     }
26 
27     @Before("Pointcut()")
28     public void before(JoinPoint joinPoint){
29         Object args[] = joinPoint.getArgs();
30         MethodSignature sig = (MethodSignature)joinPoint.getSignature();
31         Method method = sig.getMethod();
32         if(null != method.getDeclaringClass().getName() && null != method.getName() && null != args && args.length > 0){
33             log.info("{} . {} : 请求参数:{}",method.getDeclaringClass().getName(),method.getName(), StringUtils.join(args," : "));
34         }
35     }
36 
37     @AfterReturning(value="Pointcut()",returning="rvt")
38     public void after(JoinPoint joinPoint,Object rvt){
39         MethodSignature sig1 = (MethodSignature)joinPoint.getSignature();
40         Method method1 = sig1.getMethod();
41         if(null != rvt && null != method1.getDeclaringClass()){
42             try{
43                 log.info("{} . {} : 返回数据:{}",method1.getDeclaringClass().getName(),method1.getName(),new Gson().toJson(rvt));
44             }catch (Exception e){
45 
46             }
47         }
48     }
49 
50 }
<!-- SpringBoot 拦截器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

原文地址:https://blog.csdn.net/sai739295732/article/details/81741417?depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1&utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1

猜你喜欢

转载自www.cnblogs.com/dlxutianshang/p/12750780.html
今日推荐