Spring MVC 4.2.4.RELEASE 中文文档
  • Introduction
  • Part I: 目录与翻译注记(Table of contents and Notes)
    • 目录
    • 翻译注记
  • Part II: 文档内容(Documentation contents)
    • Spring Web MVC框架简介
      • Spring Web MVC的新特性
      • 允许其他MVC实现
    • DispatcherServlet
      • WebApplicationContext中特殊的Bean类型
      • 默认的DispatcherServlet配置
      • DispatcherServlet的处理流程
    • 控制器的实现
      • 使用@Controller注解定义一个控制器
      • 使用@RequestMapping注解映射请求路径
      • 定义@RequestMapping注解的处理方法
      • 异步请求的处理
      • 对控制器测试
    • 处理器映射
      • 使用HandlerInterceptor拦截请求
    • 视图解析
      • 使用ViewResolver接口解析视图
      • 视图链
      • 视图重定向
      • 内容协商解析器ContentNegotiatingViewResolver
    • 使用闪存属性FlashAttributes
    • URI构造
      • 为控制器和方法指定URI
      • 在视图中为控制器和方法指定URI
    • 地区信息
      • 获取时区信息
      • Accept请求头解析器AcceptHeaderLocaleResolver
      • Cookie解析器CookieLocaleResolver
      • Session解析器SessionLocaleResolver
      • 地区更改拦截器LocaleChangeInterceptor
    • 主题 themes
      • 关于主题:概览
      • 定义主题
      • 主题解析器
    • Spring的multipart(文件上传)支持
      • 概述
      • 使用MultipartResolver与Commons FileUpload传输文件
      • Servlet 3.0下的MultipartResolver
      • 处理表单中的文件上传
      • 处理客户端发起的文件上传请求
    • 异常处理
      • 处理器异常解析器HandlerExceptionHandler
      • @ExceptionHandler注解
      • 处理一般的Spring MVC异常
      • 使用@ResponseStatus注解业务异常
      • Servlet默认容器错误页面的定制化
    • Web安全
    • "约定优于配置"的支持
      • 控制器类名-处理器映射ControllerClassNameHandlerMapping
      • 模型ModelMap(ModelAndView)
      • 视图-请求与视图名的映射
    • HTTP缓存支持
      • HTTP请求头Cache-Control
      • 对静态资源的HTTP缓存支持
      • 在控制器中设置Cache-Control、ETag和Last-Modified响应头
      • 弱ETag
    • 基于代码的Servlet容器初始化
    • 配置Spring MVC
      • 启用MVC Java编程配置或MVC命名空间
      • 默认配置的定制化
      • 转换与格式化
      • 验证
      • 拦截器
      • 内容协商
      • 视图控制器
      • 视图解析器
      • 资源的服务
      • 回到默认的Servlet来进行资源服务
      • 路径匹配
      • 消息转换器
      • 使用MVC Java编程进行高级定制
      • 使用MVC命名空间进行高级定制
Powered by GitBook
On this page

Was this helpful?

  1. Part II: 文档内容(Documentation contents)
  2. 异常处理

处理一般的Spring MVC异常

处理请求的过程中,Spring MVC可能会抛出一些的异常。SimpleMappingExceptionResolver可以根据需要很方便地将任何异常映射到一个默认的错误视图。但,如果客户端是通过自动检测响应的方式来分发处理异常的,那么后端就需要为响应设置对应的状态码。根据抛出异常的类型不同,可能需要设置不同的状态码来标识是客户端错误(4xx)还是服务器端错误(5xx)。

默认处理器异常解析器DefaultHandlerExceptionResolver会将Spring MVC抛出的异常转换成对应的错误状态码。该解析器在MVC命名空间配置或MVC Java配置的方式下默认已经被注册了,另外,通过DispatcherServlet注册也是可行的(即不使用MVC命名空间或Java编程方式进行配置的时候)。下表列出了该解析器能处理的一些异常,及他们对应的状态码。

异常

HTTP状态码

BindException

400 (无效请求)

ConversionNotSupportedException

500 (服务器内部错误)

HttpMediaTypeNotAcceptableException

406 (不接受)

HttpMediaTypeNotSupportedException

415 (不支持的媒体类型)

HttpMessageNotReadableException

400 (无效请求)

HttpMessageNotWritableException

500 (服务器内部错误)

HttpRequestMethodNotSupportedException

405 (不支持的方法)

MethodArgumentNotValidException

400 (无效请求)

MissingServletRequestParameterException

400 (无效请求)

MissingServletRequestPartException

400 (无效请求)

NoHandlerFoundException

404 (请求未找到)

NoSuchRequestHandlingMethodException

404 (请求未找到)

TypeMismatchException

400 (无效请求)

MissingPathVariableException

500 (服务器内部错误)

NoHandlerFoundException

404 (请求未找到)

以下待翻译。

The DefaultHandlerExceptionResolver works transparently by setting the status of the response. However, it stops short of writing any error content to the body of the response while your application may need to add developer- friendly content to every error response for example when providing a REST API. You can prepare a ModelAndView and render error content through view resolution -- i.e. by configuring a ContentNegotiatingViewResolver, MappingJackson2JsonView, and so on. However, you may prefer to use @ExceptionHandler methods instead.

If you prefer to write error content via @ExceptionHandler methods you can extend ResponseEntityExceptionHandler instead. This is a convenient base for @ControllerAdvice classes providing an @ExceptionHandler method to handle standard Spring MVC exceptions and return ResponseEntity. That allows you to customize the response and write error content with message converters. See the ResponseEntityExceptionHandler javadocs for more details.

Previous@ExceptionHandler注解Next使用@ResponseStatus注解业务异常

Last updated 5 years ago

Was this helpful?