我已经查看了堆栈溢出上的一堆现有页面,但这些都没有帮助:
How to customize @RequestParam error 400 response in Spring MVC
How to inform callers when required rquest parameter is missing?
我的问题非常相似:
因此,当缺少参数时,它不会执行任何操作。当我得到一个实际的异常(即帐户不存在)时,它就会捕获异常并作为例外进行工作。这使我认为没有抛出MissingServletRequestParameterException
异常,根据文档、博客和堆栈溢出页面,应该抛出.
我还尝试实现了一个类,它扩展了DefaultHandlerExceptionResolver
并覆盖了handleMissingServletRequestParameter
方法,但没有取得多大的成功(跟随这个博客:http://alexcuesta.wordpress.com/2011/05/11/error-handling-and-http-status-codes-with-spring-mvc/ )
你知不知道我做错了什么,或者我应该探索什么其他选择?
发布于 2014-04-23 16:34:33
尝试在handleMissingServletRequestParameter
类中重写BWSExceptionHandler方法。
@ControllerAdvice
public class BWSExceptionHandler extends ResponseEntityExceptionHandler {
...
@Override
protected ResponseEntity<Object> handleMissingServletRequestParameter(
MissingServletRequestParameterException ex, HttpHeaders headers,
HttpStatus status, WebRequest request) {
// MissingServletRequestParameterException handling code goes here.
}
...
@ExceptionHandler(value = { Exception.class } )
public ResponseEntity<Object> handleOtherExceptions(final Exception ex,
final WebRequest req) {
...
}
}
希望这能有所帮助。
https://stackoverflow.com/questions/23244471
复制相似问题