Spring Aspectj @Before all rest方法

Spring Aspectj @Before all rest method

在spring引入@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,..}Mapping

我发现这个语法 适合我!

@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.PatchMapping)" +
    "|| @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) {}