github:https://github.com/chanjarste...

参考文档:

  • Spring Boot 1.5.4.RELEASE Documentation
  • Spring framework 4.3.9.RELEASE Documentation
  • Exception Handling in Spring MVC

默认行为

根据Spring Boot官方文档的说法:

For machine clients it will produce a JSON response with details of the error, the HTTP status and the exception message. For browser clients there is a ‘whitelabel’ error view that renders the same data in HTML format

也就是说,当发生异常时:

  • 如果请求是从浏览器发送出来的,那么返回一个Whitelabel Error Page
  • 如果请求是从machine客户端发送出来的,那么会返回相同信息的json

你可以在浏览器中依次访问以下地址:

  1. http://localhost:8080/return-model-and-view
  2. http://localhost:8080/return-view-name
  3. http://localhost:8080/return-view
  4. http://localhost:8080/return-text-plain
  5. http://localhost:8080/return-json-1
  6. http://localhost:8080/return-json-2

会发现FooController和FooRestController返回的结果都是一个Whitelabel Error Page也就是html。

但是如果你使用curl访问上述地址,那么返回的都是如下的json

{"timestamp": 1498886969426,"status": 500,"error": "Internal Server Error","exception": "me.chanjar.exception.SomeException","message": "...","trace": "...","path": "..."
}

但是有一个URL除外:http://localhost:8080/return-text-plain,它不会返回任何结果,原因稍后会有说明。

本章节代码在me.chanjar.boot.def,使用DefaultExample运行。

注意:我们必须在application.properties添加server.error.include-stacktrace=always才能够得到stacktrace。

Spring MVC处理请求的总体流程

分析为何浏览器访问都Whitelabel Error Page

分析为何curl text/plain资源却没有返回结果

如果你在logback-spring.xml里一样配置了这么一段:

<logger name="org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod" level="TRACE"/>

那么你就能在日志文件里发现这么一个异常:

... TRACE 13387 --- [nio-8080-exec-2] .w.s.m.m.a.ServletInvocableHandlerMethod : Invoking 'org.springframework.boot.autoconfigure.web.BasicErrorController.error' with arguments [org.apache.catalina.core.ApplicationHttpRequest@1408b81]
... TRACE 13387 --- [nio-8080-exec-2] .w.s.m.m.a.ServletInvocableHandlerMethod : Method [org.springframework.boot.autoconfigure.web.BasicErrorController.error] returned [<500 Internal Server Error,{timestamp=Thu Nov 09 13:20:15 CST 2017, status=500, error=Internal Server Error, exception=me.chanjar.exception.SomeException, message=No message available, trace=..., path=/return-text-plain, {}>]
... TRACE 13387 --- [nio-8080-exec-2] .w.s.m.m.a.ServletInvocableHandlerMethod : Error handling return value [type=org.springframework.http.ResponseEntity] [value=<500 Internal Server Error,{timestamp=Thu Nov 09 13:20:15 CST 2017, status=500, error=Internal Server Error, exception=me.chanjar.exception.SomeException, message=No message available, trace=..., path=/return-text-plain, {}>]
HandlerMethod details:
Controller [org.springframework.boot.autoconfigure.web.BasicErrorController]
Method [public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)]
org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
...

要理解这个异常是怎么来的,那我们来简单分析以下Spring MVC的处理过程:

那么这个问题怎么解决呢?我会在自定义ErrorController里说明。

自定义Error页面

前面看到了,Spring Boot针对浏览器发起的请求的error页面是Whitelabel Error Page,下面讲解如何自定义error页面。

注意2:自定义Error页面不会影响machine客户端的输出结果

方法1

根据Spring Boot官方文档,如果想要定制这个页面只需要:

to customize it just add a View that resolves to ‘error’

这句话讲的不是很明白,其实只要看ErrorMvcAutoConfiguration.WhitelabelErrorViewConfiguration的代码就知道,只需注册一个名字叫做errorView类型的Bean就行了。

本例的CustomDefaultErrorViewConfiguration注册将error页面改到了templates/custom-error-page/error.html上。

本章节代码在me.chanjar.boot.customdefaulterrorview,使用CustomDefaultErrorViewExample运行。

方法2

方法2比方法1简单很多,在Spring官方文档中没有说明。其实只需要提供error View所对应的页面文件即可。

比如在本例里,因为使用的是Thymeleaf模板引擎,所以在classpath /templates放一个自定义的error.html就能够自定义error页面了。

本章节就不提供代码了,有兴趣的你可以自己尝试。

自定义Error属性

前面看到了不论error页面还是error json,能够得到的属性就只有:timestamp、status、error、exception、message、trace、path。

如果你想自定义这些属性,可以如Spring Boot官方文档所说的:

simply add a bean of type ErrorAttributes to use the existing mechanism but replace the contents

ErrorMvcAutoConfiguration.errorAttributes提供了DefaultErrorAttributes,我们也可以参照这个提供一个自己的CustomErrorAttributes覆盖掉它。

如果使用curl访问相关地址可以看到,返回的json里的出了修改过的属性,还有添加的属性:

{"exception": "customized exception","add-attribute": "add-attribute","path": "customized path","trace": "customized trace","error": "customized error","message": "customized message","timestamp": 1498892609326,"status": 100
}

本章节代码在me.chanjar.boot.customerrorattributes,使用CustomErrorAttributesExample运行。

自定义ErrorController

在前面提到了curl http://localhost:8080/return-text-plain得不到error信息,解决这个问题有两个关键点:

  1. 请求的时候指定Accept头,避免匹配到BasicErrorController.error方法。比如:curl -H 'Accept: text/plain' http://localhost:8080/return-text-plain
  2. 提供自定义的ErrorController提供一个path=/error procudes=text/plain的方法。

其实还有另一种方式:提供一个Object->String转换的HttpMessageConverter,这个方法本文不展开。

下面将如何提供自定义的ErrorController。按照Spring Boot官方文档的说法:

To do that just extend BasicErrorController and add a public method with a @RequestMapping that has a produces attribute, and create a bean of your new type.

所以我们提供了一个CustomErrorController,并且通过CustomErrorControllerConfiguration将其注册为Bean。

本章节代码在me.chanjar.boot.customerrorcontroller,使用CustomErrorControllerExample运行。

ControllerAdvice定制特定异常返回结果

根据Spring Boot官方文档的例子,可以使用@ControllerAdvice和@ExceptionHandler对特定异常返回特定的结果。

我们在这里定义了一个新的异常:AnotherException,然后在BarControllerAdvice中对SomeException和AnotherException定义了不同的@ExceptionHandler:

  • SomeException都返回到controlleradvice/some-ex-error.html
  • AnotherException统统返回ResponseEntity

在BarController中,所有*-a都抛出SomeException,所有*-b都抛出AnotherException。下面是用浏览器和curl访问的结果:

url Browser curl
http://localhost:8080/bar/html-a some-ex-error.html some-ex-error.html
http://localhost:8080/bar/html-b error(json) error(json)
http://localhost:8080/bar/json-a some-ex-error.html some-ex-error.html
http://localhost:8080/bar/json-b error(json) error(json)
http://localhost:8080/bar/text-plain-a some-ex-error.html some-ex-error.html
http://localhost:8080/bar/text-plain-b Could not find acceptable representation(White Error Page) Could not find acceptable representation(无输出)

注意上方表格的Could not find acceptable representation错误,产生这个的原因前面已经讲过。

不过需要注意的是流程稍微有点不同,在前面的例子里的流程是这样的:

  1. 访问url
  2. 抛出异常
  3. forward到 /error
  4. BasicErrorController.error方法返回的ResponseEntity没有办法转换成String

本章节例子的异常是这样的:

  1. 访问url
  2. 抛出异常
  3. @ExceptionHandler处理
  4. AnotherException的@ExceptionHander返回的ResponseEntity没有办法转换成String,被算作没有被处理成功
  5. forward到 /error
  6. BasicErrorController.error方法返回的ResponseEntity没有办法转换成String

所以你会发现如果使用@ExceptionHandler,那就得自己根据请求头Accept的不同而输出不同的结果了,办法就是定义一个void @ExceptionHandler,具体见@ExceptionHandler javadoc。

定制不同Status Code的错误页面

Spring Boot 官方文档提供了一种简单的根据不同Status Code跳到不同error页面的方法,见这里。

我们可以将不同的Status Code的页面放在classpath: public/errorclasspath: templates/error目录下,比如400.html5xx.html400.ftl5xx.ftl

打开浏览器访问以下url会获得不同的结果:

url Result
http://localhost:8080/loo/error-403 static resource: public/error/403.html
http://localhost:8080/loo/error-406 thymeleaf view: templates/error/406.html
http://localhost:8080/loo/error-600 Whitelabel error page
http://localhost:8080/loo/error-601 thymeleaf view: templates/error/6xx.html

注意/loo/error-600返回的是Whitelabel error page,但是/loo/error-403loo/error-406能够返回我们期望的错误页面,这是为什么?先来看看代码。

loo/error-403中,我们抛出了异常Exception403

@ResponseStatus(HttpStatus.FORBIDDEN)
public class Exception403 extends RuntimeException

loo/error-406中,我们抛出了异常Exception406

@ResponseStatus(NOT_ACCEPTABLE)
public class Exception406 extends RuntimeException

注意到这两个异常都有@ResponseStatus注解,这个是注解标明了这个异常所对应的Status Code。
但是在loo/error-600中抛出的SomeException没有这个注解,而是尝试在Response.setStatus(600)来达到目的,但结果是失败的,这是为什么呢?:

@RequestMapping("/error-600")
public String error600(HttpServletRequest request, HttpServletResponse response) throws SomeException {request.setAttribute(WebUtils.ERROR_STATUS_CODE_ATTRIBUTE, 600);response.setStatus(600);throw new SomeException();
}

要了解为什么就需要知道Spring MVC对于异常的处理机制,下面简单讲解一下:

Spring MVC处理异常的地方在DispatcherServlet.processHandlerException,这个方法会利用HandlerExceptionResolver来看异常应该返回什么ModelAndView

目前已知的HandlerExceptionResolver有这么几个:

  1. DefaultErrorAttributes,只负责把异常记录在Request attributes中,name是org.springframework.boot.autoconfigure.web.DefaultErrorAttributes.ERROR
  2. ExceptionHandlerExceptionResolver,根据@ExceptionHandler resolve
  3. ResponseStatusExceptionResolver,根据@ResponseStatus resolve
  4. DefaultHandlerExceptionResolver,负责处理Spring MVC标准异常

Exception403Exception406都有被ResponseStatusExceptionResolver处理了,而SomeException没有任何Handler处理,这样DispatcherServlet就会将这个异常往上抛至到容器处理(见DispatcherServlet#L1243),以Tomcat为例,它在StandardHostValve#L317、StandardHostValve#L345会将Status Code设置成500,然后forward到/error,结果就是BasicErrorController处理时就看到Status Code=500,然后按照500去找error page找不到,就只能返回White error page了。

实际上,从Request的attributes角度来看,交给BasicErrorController处理时,和容器自己处理时,有几个相关属性的内部情况时这样的:

Attribute name When throw up to Tomcat Handled by HandlerExceptionResolver
DefaultErrorAttributes.ERROR Has value Has Value
DispatcherServlet.EXCEPTION No value Has Value
javax.servlet.error.exception Has value No Value

PS. DefaultErrorAttributes.ERROR = org.springframework.boot.autoconfigure.web.DefaultErrorAttributes.ERROR

PS. DispatcherServlet.EXCEPTION = org.springframework.web.servlet.DispatcherServlet.EXCEPTION

解决办法有两个:

  1. SomeException添加@ResponseStatus,但是这个方法有两个局限:

    1. 如果这个异常不是你能修改的,比如在第三方的Jar包里
    2. 如果@ResponseStatus使用HttpStatus作为参数,但是这个枚举定义的Status Code数量有限
  2. 使用@ExceptionHandler,不过得注意自己决定view以及status code

第二种解决办法的例子loo/error-601,对应的代码:

@RequestMapping("/error-601")
public String error601(HttpServletRequest request, HttpServletResponse response) throws AnotherException {throw new AnotherException();
}@ExceptionHandler(AnotherException.class)
String handleAnotherException(HttpServletRequest request, HttpServletResponse response, Model model)throws IOException {// 需要设置Status Code,否则响应结果会是200response.setStatus(601);model.addAllAttributes(errorAttributes.getErrorAttributes(new ServletRequestAttributes(request), true));return "error/6xx";
}

总结:

  1. 没有被HandlerExceptionResolverresolve到的异常会交给容器处理。已知的实现有(按照顺序):

    1. DefaultErrorAttributes,只负责把异常记录在Request attributes中,name是org.springframework.boot.autoconfigure.web.DefaultErrorAttributes.ERROR
    2. ExceptionHandlerExceptionResolver,根据@ExceptionHandler resolve
    3. ResponseStatusExceptionResolver,根据@ResponseStatus resolve
    4. DefaultHandlerExceptionResolver,负责处理Spring MVC标准异常
  2. @ResponseStatus用来规定异常对应的Status Code,其他异常的Status Code由容器决定,在Tomcat里都认定为500(StandardHostValve#L317、StandardHostValve#L345)
  3. @ExceptionHandler处理的异常不会经过BasicErrorController,需要自己决定如何返回页面,并且设置Status Code(如果不设置就是200)
  4. BasicErrorController会尝试根据Status Code找error page,找不到的话就用Whitelabel error page

本章节代码在me.chanjar.boot.customstatuserrorpage,使用CustomStatusErrorPageExample运行。

利用ErrorViewResolver来定制错误页面

前面讲到BasicErrorController会根据Status Code来跳转对应的error页面,其实这个工作是由DefaultErrorViewResolver完成的。

实际上我们也可以提供自己的ErrorViewResolver来定制特定异常的error页面。

@Component
public class SomeExceptionErrorViewResolver implements ErrorViewResolver {@Overridepublic ModelAndView resolveErrorView(HttpServletRequest request, HttpStatus status, Map<String, Object> model) {return new ModelAndView("custom-error-view-resolver/some-ex-error", model);}}

不过需要注意的是,无法通过ErrorViewResolver设定Status Code,Status Code由@ResponseStatus或者容器决定(Tomcat里一律是500)。

本章节代码在me.chanjar.boot.customerrorviewresolver,使用CustomErrorViewResolverExample运行。

@ExceptionHandler 和 @ControllerAdvice

前面的例子中已经有了对@ControllerAdvice和@ExceptionHandler的使用,这里只是在做一些补充说明:

  1. @ExceptionHandler配合@ControllerAdvice用时,能够应用到所有被@ControllerAdvice切到的Controller
  2. @ExceptionHandler在Controller里的时候,就只会对那个Controller生效

最佳实践

前面讲了那么多种方式,那么在Spring MVC中处理异常的最佳实践是什么?在回答这个问题前我先给出一个好的异常处理应该是什么样子的:

  1. 返回的异常信息能够适配各种Accept,比如Accept:text/html返回html页面,Accept:application/json返回json。
  2. 统一的异常信息schema,且可自定义,比如只包含timestamperrormessage等信息。
  3. 能够自定义部分信息,比如可以自定义errormessage的内容。

要达成以上目标我们可以采取的方法:

  1. 达成第1条:自定义ErrorController,扩展BasicErrorController,支持更多的Accept类型。
  2. 达成第2条:自定义ErrorAttributes
  3. 达成第3条:

    1. 使用@ResponseStatusResponseStatusException(since 5.0)
    2. 前一种方式不适用时,自定义ErrorAttributes,在里面写代码,针对特定异常返回特定信息。推荐使用配置的方式来做,比如配置文件里写XXXException的message是YYYY。

Spring MVC对于从Controller抛出的异常是不打印到console的,解决办法是提供一个HandlerExceptionResolver,比如这样:

@Order(Ordered.HIGHEST_PRECEDENCE)
public class ErrorLogger implements HandlerExceptionResolver {private static final Logger LOGGER = LoggerFactory.getLogger(ErrorLogger.class);@Overridepublic ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,Exception ex) {LOGGER.error("Exception happened at [{}]: {}", request.getRequestURI(), ExceptionUtils.getStackTrace(ex));return null;}}

附录I

下表列出哪些特性是Spring Boot的,哪些是Spring MVC的:

Feature Spring Boot Spring MVC
BasicErrorController Yes
ErrorAttributes Yes
ErrorViewResolver Yes
@ControllerAdvice Yes
@ExceptionHandler Yes
@ResponseStatus Yes
HandlerExceptionResolver Yes

Spring Boot Spring MVC 异常处理的N种方法 1相关推荐

  1. Spring Boot Spring MVC 异常处理的N种方法

    默认行为 根据Spring Boot官方文档的说法: For machine clients it will produce a JSON response with details of the e ...

  2. eclipse创建springboot项目_创建一个 Spring Boot 项目,你会几种方法?

    我最早是 2016 年底开始写 Spring Boot 相关的博客,当时使用的版本还是 1.4.x ,文章发表在 CSDN 上,阅读量最大的一篇有 42W+,如下图: 2017 年由于种种原因,就没有 ...

  3. Spring Boot 优雅停止服务的几种方法

    作者 | 黄青石 来源 | https://www.cnblogs.com/huangqingshi/p/11370291.html 最近突然想到了优雅停止 SpringBoot 服务问题,在使用 S ...

  4. spring boot + spring mvc 原理解析

    前言:spring mvc 是当前最为流行的一种java WEB 框架.在还没有spring boot以前,通常搭配tomcat等容器进行web项目的开发.而现在spring全家桶越来越完善.慢慢脱离 ...

  5. 在controller中调用指定参数给指定表单_第005课:Spring Boot 中MVC支持

    Spring Boot 的 MVC 支持主要介绍实际项目中最常用的几个注解,包括 @RestController. @RequestMapping.@PathVariable.@RequestPara ...

  6. Spring Boot入门——全局异常处理

    Spring Boot入门--全局异常处理 参考文章: (1)Spring Boot入门--全局异常处理 (2)https://www.cnblogs.com/studyDetail/p/702758 ...

  7. 基于spring boot的统一异常处理

    基于spring boot的统一异常处理 参考文章: (1)基于spring boot的统一异常处理 (2)https://www.cnblogs.com/knyel/p/7804237.html 备 ...

  8. Spring Boot学习——统一异常处理

    Spring Boot学习--统一异常处理 参考文章: (1)Spring Boot学习--统一异常处理 (2)https://www.cnblogs.com/aston/p/7258834.html ...

  9. Spring Boot 中密码加密的两种姿势!

    先说一句:密码是无法解密的.大家也不要再问松哥微人事项目中的密码怎么解密了! 密码无法解密,还是为了确保系统安全.今天松哥就来和大家聊一聊,密码要如何处理,才能在最大程度上确保我们的系统安全. 本文是 ...

最新文章

  1. Ubuntu 安装 ffmpeg
  2. Python time库的使用总结
  3. java程序设计pdf下载_Java程序设计(迟立颖) PDF扫描版[21MB]
  4. 如何评价软件写的好还是坏?
  5. 计算机职称考试题目做完会有提示么,取得计算机职称的考试心得
  6. VTK:图片之ImageDifference
  7. 空间波(space wave)
  8. spring c3p0 mysql_spring boot整合mybatis使用c3p0数据源连接mysql
  9. 想转行做web前端工程师,必学这6大技能
  10. 点点滴滴——变量对象的产生
  11. Msql自定义函数和存储过程
  12. MessageBox的用法
  13. vscode、windows快捷键
  14. KeyError: 'labels [189] not contained in axis' Python DataFrame 合并后使用loc进行索引的时候出错问题分析以及解决方案
  15. 一文了解生物识别技术
  16. 游戏建模的常用的软件和建模师的日常是什么?
  17. win10+python开发django项目day03
  18. 制作ubuntu引导盘,到安装Ubuntu系统流程
  19. android开发视频资源 电驴10G下载
  20. 如何在VMware Workstation虚拟机上安装苹果系统(Mac OS)

热门文章

  1. Xmanager企业版激活成功全过程
  2. leetcode 202. Happy Number
  3. 设计模式C#实现(十六)——中介者模式
  4. webapp 中为span元素赋值
  5. Symfony2Book03:使用Symfony2创建页
  6. PHP 做图片锐化处理
  7. django的权限认证:登录和退出。auth模块和@login_required装饰器
  8. Js组件layer的使用
  9. 字符串GZIP压缩解压
  10. Java不确定参数个数的函数方法,实现求多个数最小值