AspectJ Pointcut invokes JAX-RS annotated interface method

I am trying to intercept an interface method recorded using JAX-RS @POST. My Pointcut works for all non-interface methods, and if @ POST-Annotation is directly in the called method.

Interface method to intercept:

@POST Response postToConnector(@Context CallContext callContext, String contentStream) throws Exception; 

Pointcut to match the method:

 @Pointcut("call(@(javax.ws.rs.DELETE || javax.ws.rs.GET || javax.ws.rs.HEAD || javax.ws.rs.OPTIONS || " + "javax.ws.rs.POST || javax.ws.rs.PUT) public * org.myapp..webapi..*(..))") public void anyPublicWebApiPointcut() { ... } 

The interface is inside the com.myapp.social.webapi.v1 package, and even if I change the method to public AspectJ will not intercept the call.

Can anything be changed in my Pointcut? How can I make this work?

0
source share
1 answer

What pointcut call() does is, as the name suggests, intercepts calls to a specific method / constructor. For this to work, the caller (i.e. the code fragment where the call is located) must be under your control, i.e. It must be gossip. So, if, for example, you wove the org.myapp..webapi..* classes and the call was also issued from there, it should work. The fact that this does not work makes me assume that POST calls are coming from somewhere outside the woven code, for example. JRE or a third-party library.

So, if org.myapp..webapi..* is under your control, i.e. you can insert aspect code into it, you must use pointcut execution() . Unlike call() it is woven into the called, that is, into the code where the method is defined, and not in many places where it is called. Thus, you can intercept all execution methods, regardless of whether they come from your application or from a third-party or JRE code. This will even work to execute methods called reflection.

call() and execution() have fundamentally different semantics that pay off in order to learn and understand. Generally, you should try to use execution() whenever possible, i.e. Whenever the called person is available to you. call() is just your recession if you cannot intertwine with the called user and use the caller. call() may also make sense if for some reason you need to make any decisions based on the context of the join point, for example. in the around() advice, which decides to call or not to call the original method based on some condition.

0
source

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


All Articles