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 4.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. # spring-boot-demo-exceptionhandler
  2. 依赖[spring-boot-demo-parent](../spring-boot-demo-parent)、`spring-boot-starter-thymeleaf`(关于 thymeleaf 的会在后面的模板引擎专门有demo)
  3. ### pom.xml
  4. ```xml
  5. <?xml version="1.0" encoding="UTF-8"?>
  6. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  7. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  8. <modelVersion>4.0.0</modelVersion>
  9. <artifactId>spring-boot-demo-exceptionhandler</artifactId>
  10. <version>0.0.1-SNAPSHOT</version>
  11. <packaging>jar</packaging>
  12. <name>spring-boot-demo-exceptionhandler</name>
  13. <description>Demo project for Spring Boot</description>
  14. <parent>
  15. <groupId>com.xkcoding</groupId>
  16. <artifactId>spring-boot-demo-parent</artifactId>
  17. <version>0.0.1-SNAPSHOT</version>
  18. <relativePath>../spring-boot-demo-parent/pom.xml</relativePath>
  19. </parent>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  24. </dependency>
  25. </dependencies>
  26. <build>
  27. <finalName>spring-boot-demo-exceptionhandler</finalName>
  28. </build>
  29. </project>
  30. ```
  31. ### application.yml
  32. ```yaml
  33. server:
  34. port: 8080
  35. context-path: /demo
  36. spring:
  37. thymeleaf:
  38. mode: HTML5
  39. encoding: UTF-8
  40. content-type: text/html
  41. cache: false
  42. ```
  43. ### DemoExceptionHandler.java
  44. ```java
  45. /**
  46. * 自定义异常统一处理
  47. *
  48. * @package: com.xkcoding.springbootdemoexceptionhandler.handler
  49. * @description: 自定义异常统一处理
  50. * @author: yangkai.shen
  51. * @date: Created in 2017/11/24 下午1:37
  52. * @copyright: Copyright (c) 2017
  53. * @version: 0.0.1
  54. * @modified: yangkai.shen
  55. */
  56. @ControllerAdvice
  57. @Slf4j
  58. public class DemoExceptionHandler {
  59. public static final String DEFAULT_ERROR_VIEW = "error";
  60. /**
  61. * 统一 json 异常处理
  62. *
  63. * @param exception DemoJsonException
  64. * @return 统一返回 json 格式
  65. */
  66. @ExceptionHandler(value = DemoJsonException.class)
  67. @ResponseBody
  68. public R jsonErrorHandler(DemoJsonException exception) {
  69. log.error("【DemoJsonException】:{}", exception.getMessage());
  70. return R.error(exception);
  71. }
  72. /**
  73. * 统一 页面 异常处理
  74. *
  75. * @param exception DemoPageException
  76. * @return 统一跳转到异常页面
  77. */
  78. @ExceptionHandler(value = DemoPageException.class)
  79. public ModelAndView pageErrorHandler(DemoPageException exception) {
  80. log.error("【DemoPageException】:{}", exception.getMessage());
  81. ModelAndView view = new ModelAndView();
  82. view.addObject("message", exception.getMessage());
  83. view.setViewName(DEFAULT_ERROR_VIEW);
  84. return view;
  85. }
  86. }
  87. ```
  88. ### R.java
  89. ```java
  90. /**
  91. * 统一返回的 json 对象
  92. *
  93. * @package: com.xkcoding.springbootdemoexceptionhandler
  94. * @description: 统一返回的 json 对象
  95. * @author: yangkai.shen
  96. * @date: Created in 2017/11/24 下午1:42
  97. * @copyright: Copyright (c) 2017
  98. * @version: 0.0.1
  99. * @modified: yangkai.shen
  100. */
  101. @Data
  102. @Builder
  103. @NoArgsConstructor
  104. @AllArgsConstructor
  105. public class R<T> {
  106. private Integer code;
  107. private String message;
  108. private T data;
  109. public R(Status status) {
  110. this.code = status.getCode();
  111. this.message = status.getMessage();
  112. }
  113. public static R success(Integer code, String message, Object data) {
  114. return new R(code, message, data);
  115. }
  116. public static R success() {
  117. return new R(Status.OK);
  118. }
  119. public static R success(String message) {
  120. return success(message, null);
  121. }
  122. public static R success(String message, Object data) {
  123. return success(Code.SUCCESS.getCode(), message, data);
  124. }
  125. public static R error(Integer code, String message, Object data) {
  126. return new R(code, message, data);
  127. }
  128. public static R error(Integer code, String message) {
  129. return error(code, message, null);
  130. }
  131. public static R error(DemoJsonException exception) {
  132. return error(exception.getCode(), exception.getMessage());
  133. }
  134. }
  135. ```
  136. ### error.html (在目录 `resources/templates` 下)
  137. ```html
  138. <!DOCTYPE html>
  139. <html xmlns:th="http://www.thymeleaf.org">
  140. <head lang="en">
  141. <meta charset="UTF-8"/>
  142. <title>统一页面异常处理</title>
  143. </head>
  144. <body>
  145. <h1>统一页面异常处理</h1>
  146. <div th:text="${message}"></div>
  147. </body>
  148. </html>
  149. ```
  150. ### 其余代码
  151. 详情请参见本demo。

一个用来深度学习并实战 spring boot 的项目,目前总共包含 66 个集成demo,已经完成 55 个。

Contributors (1)