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.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. * @package: com.xkcoding.exception.handler.model
  64. * @description: 通用的 API 接口封装
  65. * @author: yangkai.shen
  66. * @date: Created in 2018/10/2 8:57 PM
  67. * @copyright: Copyright (c) 2018
  68. * @version: V1.0
  69. * @modified: yangkai.shen
  70. */
  71. @Data
  72. public class ApiResponse {
  73. /**
  74. * 状态码
  75. */
  76. private Integer code;
  77. /**
  78. * 返回内容
  79. */
  80. private String message;
  81. /**
  82. * 返回数据
  83. */
  84. private Object data;
  85. /**
  86. * 无参构造函数
  87. */
  88. private ApiResponse() {
  89. }
  90. /**
  91. * 全参构造函数
  92. *
  93. * @param code 状态码
  94. * @param message 返回内容
  95. * @param data 返回数据
  96. */
  97. private ApiResponse(Integer code, String message, Object data) {
  98. this.code = code;
  99. this.message = message;
  100. this.data = data;
  101. }
  102. /**
  103. * 构造一个自定义的API返回
  104. *
  105. * @param code 状态码
  106. * @param message 返回内容
  107. * @param data 返回数据
  108. * @return ApiResponse
  109. */
  110. public static ApiResponse of(Integer code, String message, Object data) {
  111. return new ApiResponse(code, message, data);
  112. }
  113. /**
  114. * 构造一个成功且带数据的API返回
  115. *
  116. * @param data 返回数据
  117. * @return ApiResponse
  118. */
  119. public static ApiResponse ofSuccess(Object data) {
  120. return ofStatus(Status.OK, data);
  121. }
  122. /**
  123. * 构造一个成功且自定义消息的API返回
  124. *
  125. * @param message 返回内容
  126. * @return ApiResponse
  127. */
  128. public static ApiResponse ofMessage(String message) {
  129. return of(Status.OK.getCode(), message, null);
  130. }
  131. /**
  132. * 构造一个有状态的API返回
  133. *
  134. * @param status 状态 {@link Status}
  135. * @return ApiResponse
  136. */
  137. public static ApiResponse ofStatus(Status status) {
  138. return ofStatus(status, null);
  139. }
  140. /**
  141. * 构造一个有状态且带数据的API返回
  142. *
  143. * @param status 状态 {@link Status}
  144. * @param data 返回数据
  145. * @return ApiResponse
  146. */
  147. public static ApiResponse ofStatus(Status status, Object data) {
  148. return of(status.getCode(), status.getMessage(), data);
  149. }
  150. /**
  151. * 构造一个异常且带数据的API返回
  152. *
  153. * @param t 异常
  154. * @param data 返回数据
  155. * @param <T> {@link BaseException} 的子类
  156. * @return ApiResponse
  157. */
  158. public static <T extends BaseException> ApiResponse ofException(T t, Object data) {
  159. return of(t.getCode(), t.getMessage(), data);
  160. }
  161. /**
  162. * 构造一个异常且带数据的API返回
  163. *
  164. * @param t 异常
  165. * @param <T> {@link BaseException} 的子类
  166. * @return ApiResponse
  167. */
  168. public static <T extends BaseException> ApiResponse ofException(T t) {
  169. return ofException(t, null);
  170. }
  171. }
  172. ```
  173. ## DemoExceptionHandler.java
  174. ```java
  175. /**
  176. * <p>
  177. * 统一异常处理
  178. * </p>
  179. *
  180. * @package: com.xkcoding.exception.handler.handler
  181. * @description: 统一异常处理
  182. * @author: yangkai.shen
  183. * @date: Created in 2018/10/2 9:26 PM
  184. * @copyright: Copyright (c) 2018
  185. * @version: V1.0
  186. * @modified: yangkai.shen
  187. */
  188. @ControllerAdvice
  189. @Slf4j
  190. public class DemoExceptionHandler {
  191. private static final String DEFAULT_ERROR_VIEW = "error";
  192. /**
  193. * 统一 json 异常处理
  194. *
  195. * @param exception JsonException
  196. * @return 统一返回 json 格式
  197. */
  198. @ExceptionHandler(value = JsonException.class)
  199. @ResponseBody
  200. public ApiResponse jsonErrorHandler(JsonException exception) {
  201. log.error("【JsonException】:{}", exception.getMessage());
  202. return ApiResponse.ofException(exception);
  203. }
  204. /**
  205. * 统一 页面 异常处理
  206. *
  207. * @param exception PageException
  208. * @return 统一跳转到异常页面
  209. */
  210. @ExceptionHandler(value = PageException.class)
  211. public ModelAndView pageErrorHandler(PageException exception) {
  212. log.error("【DemoPageException】:{}", exception.getMessage());
  213. ModelAndView view = new ModelAndView();
  214. view.addObject("message", exception.getMessage());
  215. view.setViewName(DEFAULT_ERROR_VIEW);
  216. return view;
  217. }
  218. }
  219. ```
  220. ## error.html
  221. > 位于 `src/main/resources/template` 目录下
  222. ```html
  223. <!DOCTYPE html>
  224. <html xmlns:th="http://www.thymeleaf.org">
  225. <head lang="en">
  226. <meta charset="UTF-8"/>
  227. <title>统一页面异常处理</title>
  228. </head>
  229. <body>
  230. <h1>统一页面异常处理</h1>
  231. <div th:text="${message}"></div>
  232. </body>
  233. </html>
  234. ```

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