aop@before,@afterReturning记录请求参数,返回参数,以及异常情形

结论:

1、before注解只要调用接口就可以执行,不受异常影响
2、after注解需要方法正常执行return才会执行,如果中途有异常,没有执行return,会导致这个注解不生效。此时可以通过@controllerAdvice注解处理全局异常或者@afterThrowing注解处理异常
3、@AfterReturning不能和@Around一起使用。否则会导致@AfterReturning获取不到返回值

切点常用写法

1)execution(public * (..))——表示匹配所有public方法
2)execution(
set(..))——表示所有以“set”开头的方法
3)execution(
com.xyz.service.AccountService.(..))——表示匹配所有AccountService接口的方法
4)execution(
com.xyz.service..(..))——表示匹配service包下所有的方法
5)execution(* com.xyz.service...(..))——表示匹配service包和它的子包下的方法

1、导入aop包


        
            com.alibaba
            fastjson
            1.2.72
        
        
            org.springframework.boot
            spring-boot-starter-aop
        

2、测试aop记录控制器

2.1、新建接口

@PostMapping("/test")
    public User test(@RequestBody User user) {
        user.setId("test");
        user.setName("test");
        return user;
    }

2.2、 新建aop

import javax.servlet.http.HttpServletRequest;

@Aspect
@Component
public class AopConfig {
    /**
     * 切点为controller包下的所有public方法
     */
    @Pointcut("execution(public * com.example.aop.controller..*.*(..))")
    public void aop(){}

    @Before("aop()")
    public void beforeAop(JoinPoint joinPoint) {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
        System.out.println("方法请求路径" + request.getRequestURI());
        System.out.println("请求方式" + request.getMethod());
        System.out.println("请求参数" + JSON.toJSONString(joinPoint.getArgs()));
    }

    @AfterReturning(returning = "value",pointcut = "aop()")
    public void afterAop(JoinPoint joinPoint,Object value) {
        System.out.println("返回参数" + JSON.toJSONString(value));
    }
}

2.3、 请求test地址,测试aop

image.png

aop正常获取到入参以及回参

2.4、测试集合接口

@PostMapping("/testList")
    public List testList(@RequestBody User user) {
        List list =new ArrayList<>();
        user.setId("test");
        user.setName("test");
        list.add(user);
        User user1 = new User();
        user1.setId("test1");
        user1.setName("test1");
        list.add(user1);
//        throw new RuntimeException("测试异常");
        return list;
    }
image.png

aop功能正常

2.4 、测试接口异常情况

@PostMapping("/testList")
    public List testList(@RequestBody User user) {
        List list =new ArrayList<>();
        user.setId("test");
        user.setName("test");
        list.add(user);
        User user1 = new User();
        user1.setId("test1");
        user1.setName("test1");
        list.add(user1);
        throw new RuntimeException("测试异常");
//        return list;
    }

image.png

aop入参记录正常,回参没有获取到。
这里可以通过全局异常@controllerAdvice处理异常记录,这里暂不记录

3、记录service层,只需要修改aop的@Pointcut,@Pointcut("execution(public * com.example.aop.service..(..))"),表示记录service包下所有的public方法

3.1、定义service方法

public User testUser(User user) {
        return user;
    }

3.2 定义接口,调用service

@PostMapping("/testService")
    public User testService(@RequestBody User user) {
        user.setId("test");
        user.setName("test");
        testService.testUser(user);
        return user;
    }

3.3 测试接口

image.png

aop一切正常

你可能感兴趣的:(aop@before,@afterReturning记录请求参数,返回参数,以及异常情形)