You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 3.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. # spring-boot-demo-exception-handler
  2. > 此 demo 演示了如何在Spring Boot中进行统一的异常处理,包括了两种方式的处理:第一种对常见API形式的接口进行异常处理,统一封装返回格式;第二种是对模板页面请求的异常处理,统一处理错误页面。
  3. ## 1.开发步骤
  4. ### 1.1.添加依赖
  5. ```xml
  6. <dependencies>
  7. <dependency>
  8. <groupId>com.xkcoding</groupId>
  9. <artifactId>common-tools</artifactId>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-web</artifactId>
  14. </dependency>
  15. <!-- 模板引擎,用于错误页面的展示 -->
  16. <dependency>
  17. <groupId>org.springframework.boot</groupId>
  18. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  19. </dependency>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-test</artifactId>
  23. <scope>test</scope>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.projectlombok</groupId>
  27. <artifactId>lombok</artifactId>
  28. <optional>true</optional>
  29. </dependency>
  30. </dependencies>
  31. ```
  32. ### 1.2.构造JSON异常和页面跳转异常
  33. ```java
  34. // JSON 异常
  35. @Getter
  36. public class JsonException extends CommonBizException {
  37. public JsonException(IStatus status) {
  38. super(status);
  39. }
  40. public JsonException(Integer code, String message) {
  41. super(code, message);
  42. }
  43. }
  44. // 页面异常
  45. @Getter
  46. public class PageException extends CommonBizException {
  47. public PageException(IStatus status) {
  48. super(status);
  49. }
  50. public PageException(Integer code, String message) {
  51. super(code, message);
  52. }
  53. }
  54. ```
  55. ### 1.3.异常拦截
  56. ```java
  57. @Slf4j
  58. @ControllerAdvice
  59. public class DemoExceptionHandler {
  60. private static final String DEFAULT_ERROR_VIEW = "error";
  61. /**
  62. * 统一 json 异常处理
  63. *
  64. * @param exception JsonException
  65. * @return 统一返回 json 格式
  66. */
  67. @ExceptionHandler(value = JsonException.class)
  68. @ResponseBody
  69. public Response<Void> jsonErrorHandler(JsonException exception) {
  70. log.error("【JsonException】:{}", exception.getMessage());
  71. return Response.ofError(exception);
  72. }
  73. /**
  74. * 统一 页面 异常处理
  75. *
  76. * @param exception PageException
  77. * @return 统一跳转到异常页面
  78. */
  79. @ExceptionHandler(value = PageException.class)
  80. public ModelAndView pageErrorHandler(PageException exception) {
  81. log.error("【PageException】:{}", exception.getMessage());
  82. ModelAndView view = new ModelAndView();
  83. view.addObject("message", exception.getMessage());
  84. view.setViewName(DEFAULT_ERROR_VIEW);
  85. return view;
  86. }
  87. }
  88. ```
  89. ### 1.4.编写统一错误页面
  90. > 位于 `src/main/resources/template` 目录下
  91. ```html
  92. <!DOCTYPE html>
  93. <html xmlns:th="http://www.thymeleaf.org">
  94. <head lang="en">
  95. <meta charset="UTF-8"/>
  96. <title>统一页面异常处理</title>
  97. </head>
  98. <body>
  99. <h1>统一页面异常处理</h1>
  100. <div th:text="${message}"></div>
  101. </body>
  102. </html>
  103. ```
  104. ## 2.测试
  105. ### 2.1.编写测试路由代码模拟异常
  106. ```java
  107. @Controller
  108. public class TestController {
  109. @GetMapping("/json")
  110. @ResponseBody
  111. public Response<Void> jsonException() {
  112. throw new JsonException(CommonStatus.SERVER_ERROR);
  113. }
  114. @GetMapping("/page")
  115. public ModelAndView pageException() {
  116. throw new PageException(CommonStatus.SERVER_ERROR);
  117. }
  118. }
  119. ```
  120. 启动 `ExceptionHandlerApplication`
  121. - 测试API形式的异常拦截返回,浏览器进入 `http://localhost:8080/demo/json` ,同时观察控制台日志输出
  122. - 测试统一错误页面的异常拦截返回,浏览器进入 `http://localhost:8080/demo/page` ,同时观察控制台日志输出