如何从控制器获取请求URI,Spring AOP建议中的请求方法?

问题描述 投票:0回答:1

控制器:

@PostMapping("api/postmethod")
public Response getResponse(@RequestParam String name, @RequestBody Object obj...){
....
}

方面:

@After("execution(* *(..))")
public void after(JoinPoint joinPoint){
...
}

如何在Aspect建议中获取请求uri->(“ api / postmethod”)?

aop spring-aop
1个回答
0
投票

以下代码将获得PostMapping路径

@Around("@annotation(postMapping) && within(com.aop.example.web.controller..*)")
public Object postMapping(ProceedingJoinPoint jp,PostMapping postMapping) throws Throwable {
    System.out.println(Arrays.asList(postMapping.value()));
    return jp.proceed();
}

说明:

切入点表达式@Around("@annotation(postMapping) && within(com.aop.example.web.controller..*)")可以解释如下

  • [@PostMapping和]注释的方法>
  • 在根包com.aop.example.web.controller内的任何类中>
  • 可将参数传递给建议的文档参考here

© www.soinside.com 2019 - 2024. All rights reserved.