简体   繁体   English

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

[英]Adding parameters to @ExceptionHandler for MethodArgumentNotValidException in Spring

I have a Spring controller that validates incoming requests with hibernate validator.我有一个 Spring 控制器,它使用休眠验证器验证传入的请求。

When the request is invalid, MethodArgumentNotValidException is thrown by the validator.当请求无效时,验证器会抛出MethodArgumentNotValidException异常。 Would it be possible to add additional class as an argument to handler method for the exception?是否可以添加额外的类作为异常处理程序方法的参数?

This is what i have:这就是我所拥有的:

@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);

    }
}

And i would need something like example bellow, however this doesn't work:我需要类似下面的示例,但这不起作用:

@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseBody
public Response handleInvalidRequest(MethodArgumentNotValidException e, CustomObject obj) {

 // do something with CustomObject
}

If you want to do something with the object which failed the validation in the exception handler, you can retrieve it from BindingResult like so:如果你想对异常处理程序中验证失败的对象做一些事情,你可以像这样从BindingResult检索它:

@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseBody
public Response handleInvalidRequest(MethodArgumentNotValidException e) {
    CustomObject ce = (CustomObject) e.getBindingResult().getTarget();
    // do something with CustomObject
}

You can also take a look at Spring JavaDoc for @ExceptionHandler annotation to see the list of supported exception handler method argument types:您还可以查看@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().如果注释本身没有通过其 value() 缩小异常类型,这也可用作映射提示。
  • Request and/or response objects (Servlet API or Portlet API).请求和/或响应对象(Servlet API 或 Portlet API)。 You may choose any specific request/response type, eg ServletRequest / HttpServletRequest or PortletRequest / ActionRequest / RenderRequest.您可以选择任何特定的请求/响应类型,例如 ServletRequest / HttpServletRequest 或 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).请注意,在 Portlet 情况下,显式声明的 action/render 参数也用于将特定请求类型映射到处理程序方法(在没有给出区分操作和渲染请求的其他信息的情况下)。
  • Session object (Servlet API or Portlet API): either HttpSession or PortletSession.会话对象(Servlet API 或 Portlet API):HttpSession 或 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.请注意,会话访问可能不是线程安全的,尤其是在 Servlet 环境中:如果允许多个请求同时访问一个会话,请考虑将“synchronizeOnSession”标志切换为“true”。
  • WebRequest or NativeWebRequest. WebRequest 或 NativeWebRequest。 Allows for generic request parameter access as well as request/session attribute access, without ties to the native Servlet/Portlet API.允许通用请求参数访问以及请求/会话属性访问,与本机 Servlet/Portlet API 无关。
  • Locale for the current request locale (determined by the most specific locale resolver available, ie the configured LocaleResolver in a Servlet environment and the portal locale in a Portlet environment).当前请求语言环境的语言环境(由最具体的可用语言环境解析器确定,即 Servlet 环境中配置的 LocaleResolver 和 Portlet 环境中的门户语言环境)。
  • InputStream / Reader for access to the request's content. InputStream / Reader 用于访问请求的内容。 This will be the raw InputStream/Reader as exposed by the Servlet/Portlet API.这将是 Servlet/Portlet API 公开的原始 InputStream/Reader。
  • OutputStream / Writer for generating the response's content. OutputStream / Writer 用于生成响应的内容。 This will be the raw OutputStream/Writer as exposed by the Servlet/Portlet API.这将是 Servlet/Portlet API 公开的原始 OutputStream/Writer。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring 启动 @RequestBody 和 @Valid- ExceptionHandler for MethodArgumentNotValidException - Spring boot @RequestBody with @Valid- ExceptionHandler for MethodArgumentNotValidException 永远不会调用 RestController 中 MethodArgumentNotValidException 的 Spring 引导异常处理程序 - Spring Boot ExceptionHandler for MethodArgumentNotValidException in RestController never gets invoked @ExceptionHandler没有捕获MethodArgumentNotValidException - @ExceptionHandler doesn't catch MethodArgumentNotValidException 在启动 spring 引导应用程序时获取为 MethodArgumentNotValidException 映射的不明确的 @ExceptionHandler 方法 - Getting Ambiguous @ExceptionHandler method mapped for MethodArgumentNotValidException while startup of spring boot application 在Spring中向@ExceptionHandler添加自定义消息 - Adding a custom message to a @ExceptionHandler in Spring Spring中的ExceptionHandler - ExceptionHandler in Spring 为什么 @ExceptionHandler(MethodArgumentNotValidException.class) 被忽略而支持 @ExceptionHandler(Exception.class) - Why @ExceptionHandler(MethodArgumentNotValidException.class) is ignored in favour of @ExceptionHandler(Exception.class) @exceptionhandler在Spring REST中不起作用 - @exceptionhandler not working in Spring REST Spring 3 - 为NoSuchRequestHandlingMethodException创建ExceptionHandler - Spring 3 - Create ExceptionHandler for NoSuchRequestHandlingMethodException Spring @ExceptionHandler行为 - Spring @ExceptionHandler behaviour
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM