MethodInterceptor
我对方面和@Validated
注释有一个奇怪的问题。当我添加方法拦截器来拦截所有用 注释的方法时@RequestMapping
,@Validated
注释不起作用。当我删除它时,它又可以正常工作了。我认为,我的拦截器会覆盖@Validated
. 有什么办法可以做到这一点,因为我不想在我的拦截器中执行验证,因为它是为了审计东西。
这是验证器:
@Component
public class UserValidator implements Validator {
@Override
public void validate(Object target, Errors errors) {
//validation stuff
}
}
控制器代码:
@Controller
@RequestMapping("/profile")
public class UserProfileController {
@Autowired
private UserValidator userValidator;
@InitBinder("userValidator")
private void userValidatorInitBinder(WebDataBinder binder) {
binder.setValidator(userValidator);
}
@RequestMapping(value = "/reg.do", method = RequestMethod.POST)
public @ResponseBody RegisterResponse reg(HttpServletRequest httpServletRequest, @RequestBody @Validated CreateUserRequest createUserRequest) {
}
}
拦截器代码:
@Aspect
@Component
public class MyInterceptor implements MethodInterceptor, Serializable {
@Around("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
Object returnValue = null;
final StopWatch stopWatch = new StopWatch();
try {
stopWatch.start();
returnValue = invocation.proceed();
return returnValue;
} catch (Throwable ex) {
stopStopwatch(stopWatch);
throw ex;
} finally {
System.out.println(stopwatch);
}
}
}
我的环境是带有休息控制器(杰克逊映射器)的 Spring MVC(3.2)。