结论:
SpringBoot是先执行@Valid注解再执行切面, 所以无法将AOP的触发位置移动到@Valid之前.
自定义注解如果想要在@Valid校验之前触发,要么通过拦截器, 但拦截器对参数的获取较为麻烦, 建议使用ControllerAdvice,Controller增强,然后在SpringBoot的数据绑定后的时候,去进行触发.
这样可以从WebDataBinder对象中轻松获取已经被SpringBoot绑定到实体的对象
@Slf4j
@ControllerAdvice
public class ParamDecryptHandler {
@InitBinder
public void processDecryptParam(WebDataBinder dataBinder) throws IllegalAccessException{
Object o = dataBinder.getTarget();
}
}o即为当前对象, 可以在此处对自定义注解进行解析反射修改等操作.
SpringBoot@Valid与AOP执行顺序及自定义注解处理
SpringBoot先执行@Valid再执行切面,无法改变次序。自定义注解若要提前触发,推荐使用ControllerAdvice,在数据绑定后操作。示例展示了如何在@InitBinder中通过WebDataBinder处理目标对象。

被折叠的 条评论
为什么被折叠?



