java – 在Spring中为MethodArgumentNotValidException的@ExceptionHandler添加参数
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 在Spring中为MethodArgumentNotValidException的@ExceptionHandler添加参数,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3099字,纯文字阅读大概需要5分钟。
内容图文
我有一个Spring控制器,用hibernate验证器验证传入的请求.
当请求无效时,验证程序抛出MethodArgumentNotValidException.是否可以将附加类作为参数添加到异常的处理程序方法中?
这就是我所拥有的:
@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
}
您还可以查看Spring JavaDoc for @ExceptionHandler批注,以查看支持的异常处理程序方法参数类型的列表:
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添加参数全部内容,希望文章能够帮你解决java – 在Spring中为MethodArgumentNotValidException的@ExceptionHandler添加参数所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。