我有一个 Spring Controller ,它使用 hibernate validator 验证传入的请求。
当请求无效时,MethodArgumentNotValidException
被 validator 抛出。是否可以添加额外的类作为异常处理程序方法的参数?
这是我的:
@RequestMapping(value = "/...", method = RequestMethod.POST)
@ResponseBody
public Response handleCustomObject(@Valid @RequestBody CustomObject obj) {
//..
}
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseBody
public Response handleInvalidRequest(MethodArgumentNotValidException e) {
return getMissingMandatoryParametersResponse(e);
}
}
我需要类似下面示例的东西,但这不起作用:
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseBody
public Response handleInvalidRequest(MethodArgumentNotValidException e, CustomObject obj) {
// do something with CustomObject
}
最佳答案
如果你想对异常处理程序中验证失败的对象做一些事情,你可以从 BindingResult
中检索它,如下所示:
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseBody
public Response handleInvalidRequest(MethodArgumentNotValidException e) {
CustomObject ce = (CustomObject) e.getBindingResult().getTarget();
// do something with CustomObject
}
您还可以查看 @ExceptionHandler
的 Spring JavaDoc用于查看支持的异常处理程序方法参数类型列表的注释:
Handler methods which are annotated with this annotation are allowed to have very flexible signatures. They may have arguments of the following types, in arbitrary order:
- An exception argument: declared as a general Exception or as a more specific exception. This also serves as a mapping hint if the annotation itself does not narrow the exception types through its value().
- Request and/or response objects (Servlet API or Portlet API). You may choose any specific request/response type, e.g. ServletRequest / HttpServletRequest or PortletRequest / ActionRequest / RenderRequest. Note that in the Portlet case, an explicitly declared action/render argument is also used for mapping specific request types onto a handler method (in case of no other information given that differentiates between action and render requests).
- Session object (Servlet API or Portlet API): either HttpSession or PortletSession. An argument of this type will enforce the presence of a corresponding session. As a consequence, such an argument will never be null. Note that session access may not be thread-safe, in particular in a Servlet environment: Consider switching the "synchronizeOnSession" flag to "true" if multiple requests are allowed to access a session concurrently.
- WebRequest or NativeWebRequest. Allows for generic request parameter access as well as request/session attribute access, without ties to the native Servlet/Portlet API.
- Locale for the current request locale (determined by the most specific locale resolver available, i.e. the configured LocaleResolver in a Servlet environment and the portal locale in a Portlet environment).
- InputStream / Reader for access to the request's content. This will be the raw InputStream/Reader as exposed by the Servlet/Portlet API.
- OutputStream / Writer for generating the response's content. This will be the raw OutputStream/Writer as exposed by the Servlet/Portlet API.
关于java - 在 Spring 中为 MethodArgumentNotValidException 添加参数到 @ExceptionHandler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31616546/