I have this service/controller method:
public ResponseEntity<PolicyDTO> addPolicy(@Valid @RequestBody PolicyDTO policy)
throws InternalServerException, BadRequestException {
log.debug("Adding a new policy");
}
Notice the @Valid annotation in the method params
For this controller we have defined the following aspect class:
@Aspect
@Component
public class BackwardsCompatibilityHandler {
@Around("execution(* com.company.PolicyController.addPolicy(..))")
public Object ControllerMethod(JoinPoint jp)
{
// DO Code before and after the method call
}
The main reason behind this aspect / proxy is to intercept method calls and do some pre / post processing over the input params / returned results.
The main problem that we face now is that the @Valid annotation is handled before the aspect before and after code is actually executed.
Any idea on how we can make the Validation check run inside our AOP code? I know there is a way to do this by manually calling the validator inside the method, but would like not to touch the existing code that much... so any ideas other than this, if there are...
Right now the order of execution is this:
- Validation check - triggered by the @Valid anotation
- Own code - aspect - before method call
- Actual method call
- Own code - aspect - after method call
And we would like to have something more like (first our code and then the Valid annotation):
- Own code - aspect - before method call
- Validation check - triggered by the @Valid anotation
- Actual method call
- Own code - aspect - after method call
Please skip over compiler errors or such, this code is only for demonstration purposes, it's not the real code :)
RequestMappingHandlerAdapter
. imho you really should be using aHandlerInterceptor
and operate on the request. – Zenascall()
instead ofexecution()
. Acall()
pointcut is fired before its the correspondingexecution()
pointcut because it weaves the caller, not the callee. – Primate