java - 在 Spring 中为 MethodArgumentNotValidException 添加参数到 @ExceptionHandler

标签 java spring validation spring-mvc

我有一个 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/

相关文章:

java - 当尝试使用 dynamodb 和 graphql 运行我的 Spring Boot 应用程序时,我不断收到此 @bean 错误

java - 任何带有 TaskExecutor 示例的好的 Spring 线程?

validation - 有人使用 YUI 构建表单验证吗?

java - 比较器比较类型推断

java - 如何使用API​​ 21的camera2类并使应用程序仍然运行于较低的API?

java - 使用 Spring SAML 自动重定向到 IdP

laravel - 具有自定义请求的独特值(value) Laravel 5.3

Spring MVC 表单验证不起作用

java - 相机图像处理

java - 使用 PowerMock 添加测试后 Spring Web 应用程序无法启动