Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses and challenges
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

200
Views
Spring Aspectj @Before all rest method

Before spring introduce @GetMapping, there only one annotation we care about @RequestMapping, so, this aspect works

@Before("within(aa.bb.*.rest..*) && execution(public * *(..)) && @within(org.springframework.web.bind.annotation.RestController) && @annotation(org.springframework.web.bind.annotation.RequestMapping)")

But after we can use @GetMapping, @PostMapping, this point not works, but these annotations have a meta annotation @RequestMapping.

Is there any way to easily intercept all @RequestMapping/@{Get,Post,Put,Patch,..}Mapping ?

8 months ago · Santiago Trujillo
1 answers
Answer question

0

I found this syntax here works for me!

@Pointcut("execution(@(@org.springframework.web.bind.annotation.RequestMapping *) * *(..))")
public void requestMappingAnnotations() { }

Also I can list them all

@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) {}
8 months ago · Santiago Trujillo Report
Answer question
Find remote jobs