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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. # spring-boot-demo-exception-handler
  2. > 此 demo 演示了如何在Spring Boot中进行统一的异常处理,包括了两种方式的处理:第一种对常见API形式的接口进行异常处理,统一封装返回格式;第二种是对模板页面请求的异常处理,统一处理错误页面。
  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-exception-handler</artifactId>
  10. <version>1.0.0-SNAPSHOT</version>
  11. <packaging>jar</packaging>
  12. <name>spring-boot-demo-exception-handler</name>
  13. <description>Demo project for Spring Boot</description>
  14. <parent>
  15. <groupId>com.xkcoding</groupId>
  16. <artifactId>spring-boot-demo</artifactId>
  17. <version>1.0.0-SNAPSHOT</version>
  18. </parent>
  19. <properties>
  20. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  21. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  22. <java.version>1.8</java.version>
  23. </properties>
  24. <dependencies>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.springframework.boot</groupId>
  31. <artifactId>spring-boot-starter-web</artifactId>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.springframework.boot</groupId>
  35. <artifactId>spring-boot-starter-test</artifactId>
  36. <scope>test</scope>
  37. </dependency>
  38. <dependency>
  39. <groupId>org.projectlombok</groupId>
  40. <artifactId>lombok</artifactId>
  41. <optional>true</optional>
  42. </dependency>
  43. </dependencies>
  44. <build>
  45. <finalName>spring-boot-demo-exception-handler</finalName>
  46. <plugins>
  47. <plugin>
  48. <groupId>org.springframework.boot</groupId>
  49. <artifactId>spring-boot-maven-plugin</artifactId>
  50. </plugin>
  51. </plugins>
  52. </build>
  53. </project>
  54. ```
  55. ## ApiResponse.java
  56. > 统一的API格式返回封装,里面涉及到的 `BaseException` 和`Status` 这两个类,具体代码见 demo。
  57. ```java
  58. /**
  59. * <p>
  60. * 通用的 API 接口封装
  61. * </p>
  62. *
  63. * @author yangkai.shen
  64. * @date Created in 2018-10-02 20:57
  65. */
  66. @Data
  67. public class ApiResponse {
  68. /**
  69. * 状态码
  70. */
  71. private Integer code;
  72. /**
  73. * 返回内容
  74. */
  75. private String message;
  76. /**
  77. * 返回数据
  78. */
  79. private Object data;
  80. /**
  81. * 无参构造函数
  82. */
  83. private ApiResponse() {
  84. }
  85. /**
  86. * 全参构造函数
  87. *
  88. * @param code 状态码
  89. * @param message 返回内容
  90. * @param data 返回数据
  91. */
  92. private ApiResponse(Integer code, String message, Object data) {
  93. this.code = code;
  94. this.message = message;
  95. this.data = data;
  96. }
  97. /**
  98. * 构造一个自定义的API返回
  99. *
  100. * @param code 状态码
  101. * @param message 返回内容
  102. * @param data 返回数据
  103. * @return ApiResponse
  104. */
  105. public static ApiResponse of(Integer code, String message, Object data) {
  106. return new ApiResponse(code, message, data);
  107. }
  108. /**
  109. * 构造一个成功且带数据的API返回
  110. *
  111. * @param data 返回数据
  112. * @return ApiResponse
  113. */
  114. public static ApiResponse ofSuccess(Object data) {
  115. return ofStatus(Status.OK, data);
  116. }
  117. /**
  118. * 构造一个成功且自定义消息的API返回
  119. *
  120. * @param message 返回内容
  121. * @return ApiResponse
  122. */
  123. public static ApiResponse ofMessage(String message) {
  124. return of(Status.OK.getCode(), message, null);
  125. }
  126. /**
  127. * 构造一个有状态的API返回
  128. *
  129. * @param status 状态 {@link Status}
  130. * @return ApiResponse
  131. */
  132. public static ApiResponse ofStatus(Status status) {
  133. return ofStatus(status, null);
  134. }
  135. /**
  136. * 构造一个有状态且带数据的API返回
  137. *
  138. * @param status 状态 {@link Status}
  139. * @param data 返回数据
  140. * @return ApiResponse
  141. */
  142. public static ApiResponse ofStatus(Status status, Object data) {
  143. return of(status.getCode(), status.getMessage(), data);
  144. }
  145. /**
  146. * 构造一个异常且带数据的API返回
  147. *
  148. * @param t 异常
  149. * @param data 返回数据
  150. * @param <T> {@link BaseException} 的子类
  151. * @return ApiResponse
  152. */
  153. public static <T extends BaseException> ApiResponse ofException(T t, Object data) {
  154. return of(t.getCode(), t.getMessage(), data);
  155. }
  156. /**
  157. * 构造一个异常且带数据的API返回
  158. *
  159. * @param t 异常
  160. * @param <T> {@link BaseException} 的子类
  161. * @return ApiResponse
  162. */
  163. public static <T extends BaseException> ApiResponse ofException(T t) {
  164. return ofException(t, null);
  165. }
  166. }
  167. ```
  168. ## DemoExceptionHandler.java
  169. ```java
  170. /**
  171. * <p>
  172. * 统一异常处理
  173. * </p>
  174. *
  175. * @author yangkai.shen
  176. * @date Created in 2018-10-02 21:26
  177. */
  178. @ControllerAdvice
  179. @Slf4j
  180. public class DemoExceptionHandler {
  181. private static final String DEFAULT_ERROR_VIEW = "error";
  182. /**
  183. * 统一 json 异常处理
  184. *
  185. * @param exception JsonException
  186. * @return 统一返回 json 格式
  187. */
  188. @ExceptionHandler(value = JsonException.class)
  189. @ResponseBody
  190. public ApiResponse jsonErrorHandler(JsonException exception) {
  191. log.error("【JsonException】:{}", exception.getMessage());
  192. return ApiResponse.ofException(exception);
  193. }
  194. /**
  195. * 统一 页面 异常处理
  196. *
  197. * @param exception PageException
  198. * @return 统一跳转到异常页面
  199. */
  200. @ExceptionHandler(value = PageException.class)
  201. public ModelAndView pageErrorHandler(PageException exception) {
  202. log.error("【DemoPageException】:{}", exception.getMessage());
  203. ModelAndView view = new ModelAndView();
  204. view.addObject("message", exception.getMessage());
  205. view.setViewName(DEFAULT_ERROR_VIEW);
  206. return view;
  207. }
  208. }
  209. ```
  210. ## error.html
  211. > 位于 `src/main/resources/template` 目录下
  212. ```html
  213. <!DOCTYPE html>
  214. <html xmlns:th="http://www.thymeleaf.org">
  215. <head lang="en">
  216. <meta charset="UTF-8"/>
  217. <title>统一页面异常处理</title>
  218. </head>
  219. <body>
  220. <h1>统一页面异常处理</h1>
  221. <div th:text="${message}"></div>
  222. </body>
  223. </html>
  224. ```

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