在春天介绍@GetMapping之前,只有一个注释我们关心@RequestMapping,所以,这个方面有效
@Before("within(aa.bb.*.rest..*) && execution(public * *(..)) && @within(org.springframework.web.bind.annotation.RestController) && @annotation(org.springframework.web.bind.annotation.RequestMapping)")
但是在我们可以使用@GetMapping,@ PostMapping之后,这一点不起作用,但这些注释有一个元注释@RequestMapping.
有没有办法轻松拦截所有@RequestMapping / @ {Get,Post,Put,Patch,..}映射?
解决方法:
我发现这个语法here对我有用!
@Pointcut("execution(@(@org.springframework.web.bind.annotation.RequestMapping *) * *(..))")
public void requestMappingAnnotations() { }
我也可以列出所有
@Pointcut("within(aa.bb.*.rest..*) && @within(org.springframework.web.bind.annotation.RestController)")
public void restControllers() {}
@Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping) " +
"|| @annotation(org.springframework.web.bind.annotation.GetMapping)" +
"|| @annotation(org.springframework.web.bind.annotation.PostMapping)" +
"|| @annotation(org.springframework.web.bind.annotation.PathVariable)" +
"|| @annotation(org.springframework.web.bind.annotation.PutMapping)" +
"|| @annotation(org.springframework.web.bind.annotation.DeleteMapping)"
)
public void mappingAnnotations() {}
@Pointcut("execution(@(@org.springframework.web.bind.annotation.RequestMapping *) * *(..))")
public void requestMappingAnnotations() { }
@Before("restControllers() && requestMappingAnnotations()")
public void onExecute(JoinPoint jp) {}