我正在创建一个验证库,我想在控制器之前验证请求。如果我能在拦截器中获取我想要验证的控制器参数,那就太好了。
目前我可以获得有关控制器参数的所有信息,但我找不到获取参数内部实例的方法。这就是我目前所拥有的:
public class ValidationInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (handler instanceof HandlerMethod) {
HandlerMethod method = (HandlerMethod) handler;
for (MethodParameter param: method.getMethodParameters()) {
// Check if the parameter has the right annotations.
if (param.hasParameterAnnotation(RequestBody.class) && param.hasParameterAnnotation(Valid.class)) {
// Here I wan't to get the object that is in the parameter so I can validate it.
}
}
}
return true;
}
}
示例控制器方法:
@RequestMapping(value = "register", method = RequestMethod.POST)
public Response register(@Valid @RequestBody RegisterRequest request) {
// return response and stuff.
}
注册请求:
public class RegisterRequest {
@JsonProperty("email")
public String email;
@JsonProperty("name")
public String name;
@JsonProperty("password")
public String password;
@JsonProperty("password_confirmation")
public String passwordConfirmation;
}
有没有一种简单的方法可以从拦截器访问控制器参数?