How do I get my Spring aspects to execute before @Valid/@Validated annotation on a spring controller method?

社会主义新天地 提交于 2019-12-01 16:25:28

问题


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:

  1. Validation check - triggered by the @Valid anotation
  2. Own code - aspect - before method call
  3. Actual method call
  4. Own code - aspect - after method call

And we would like to have something more like (first our code and then the Valid annotation):

  1. Own code - aspect - before method call
  2. Validation check - triggered by the @Valid anotation
  3. Actual method call
  4. 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 :)

来源:https://stackoverflow.com/questions/39271035/how-do-i-get-my-spring-aspects-to-execute-before-valid-validated-annotation-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!