跳到主要内容

Spring MVC系列(13)-实际开发中异常处理方案

Spring MVC中的异常

首先了解下 Spring MVC中定义的一些异常。

异常说明
HttpRequestMethodNotSupportedException请求处理程序不支持具体请求方法
HttpMediaTypeNotSupportedExceptionMediaType不支持
HttpMediaTypeNotAcceptableException当请求处理程序无法生成客户端可接受的MediaTyp时引发异常。
MissingPathVariableException方法在从URL提取的URI变量中不存在

| MissingServletRequestParameterException | 指示缺少参数 | | ServletRequestBindingException | 扩展ServletException | | ConversionNotSupportedException | 当找不到适用于bean属性的编辑器或转换器时引发异常。 | | TypeMismatchException | 尝试设置bean属性时,类型不匹配引发异常。 | | HttpMessageNotReadableException | 消息转换器read 时失败 | | HttpMessageNotWritableException | 消息转换器write 时失败 | | MethodArgumentNotValidException | 对带有{@code@Valid}注释的参数进行验证失败时引发的异常 | | MissingServletRequestPartException | multipart/form-data请求时,找不到对应名称 | | BindException | 绑定错误 | | NoHandlerFoundException | 当DispatcherServlet找不到请求的处理程序时,它会发送404 | | AsyncRequestTimeoutException | 异步请求超时异常 |

1. 指定4xx、5xx错误页面

思路

spring boot中的DefaultErrorViewResolver解析器负责对错误视图进行处理,不同的响应状态码,比如500异常,会解析为error/500的视图名。