How do you get RequestMapping request in AOP board from Spring controller?

For some kind of controller with query display

@RequestMapping(value="/some/path", method=RequestMethod.POST) 

How would you retrieve the method value (RequestMethod.POST) in the aspect class?

I want to keep track of all the controller methods that execute the POST request.

thanks

+5
source share
2 answers

Found a solution.

 import org.aspectj.lang.reflect.MethodSignature; import java.lang.reflect.Method; @Pointcut("within(@org.springframework.stereotype.Controller *)") public void controller() {} // In advice MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); Method method = methodSignature .getMethod(); RequestMethod[] requestMethods = method.getAnnotation(RequestMapping.class).method(); 

Be careful what class you import.

+4
source

@ AL13N: Your own answer is correct, but you do not need to use reflection if you just bind the annotation to a parameter. Here is an example in POJO + AspectJ. In Spring AOP, it should be the same:

Sample controller with the main method:

 package de.scrum_master.app; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class MyController { @RequestMapping(value="/some/path", method=RequestMethod.POST) public void foo() { System.out.println("foo"); } public void bar() { System.out.println("bar"); } public static void main(String[] args) { MyController controller = new MyController(); controller.foo(); controller.bar(); } } 

Format:

 package de.scrum_master.aspect; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.web.bind.annotation.RequestMapping; public aspect RequestMappingInterceptor { @Pointcut( "within(@org.springframework.stereotype.Controller *) && " + "@annotation(requestMapping) && " + "execution(* *(..))" ) public void controller(RequestMapping requestMapping) {} @Before("controller(requestMapping)") public void advice(JoinPoint thisJoinPoint, RequestMapping requestMapping) { System.out.println(thisJoinPoint); System.out.println(" " + requestMapping); System.out.println(" " + requestMapping.method()[0]); } } 

BTW, part of the && execution(* *(..)) pointcut, is probably not needed in Spring AOP because it just knows the pointcut execution points. In AspectJ, you need to exclude call() and other pointcut types because AspectJ is more powerful. This will not hurt, although safer and more explicit.

Console exit:

 execution(void de.scrum_master.app.MyController.foo()) @org.springframework.web.bind.annotation.RequestMapping(headers=[], name=, value=[/some/path], produces=[], method=[POST], params=[], consumes=[]) POST foo bar 

Edit: Parameters have been changed to make the join point the first parameter of the consultation method, because Spring AOP insists on this order while AspectJ does not.

+7
source

Source: https://habr.com/ru/post/1206887/


All Articles