说明
https://gitee.com/xkcoding/spring-boot-demo
此 demo 演示了如何在Spring Boot中进行统一的异常处理,包括了两种方式的处理:第一种对常见API形式的接口进行异常处理,统一封装返回格式;第二种是对模板页面请求的异常处理,统一处理错误页面
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<artifactId>spring-boot-demo-exception-handler</artifactId> <version>1.0.0-SNAPSHOT</version> <packaging>jar</packaging>
<name>spring-boot-demo-exception-handler</name> <description>Demo project for Spring Boot</description>
<parent> <groupId>com.xkcoding</groupId> <artifactId>spring-boot-demo</artifactId> <version>1.0.0-SNAPSHOT</version> </parent>
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties>
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> </dependencies>
<build> <finalName>spring-boot-demo-exception-handler</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
</project>
|
application.yml
1 2 3 4 5 6 7 8 9 10 11
| server: port: 8080 servlet: context-path: /demo spring: thymeleaf: cache: false mode: HTML encoding: UTF-8 servlet: content-type: text/html
|
ApiResponse.java
统一的API格式返回封装,里面涉及到的 BaseException
和Status
这两个类,具体代码见 demo。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
@Data public class ApiResponse {
private Integer code;
private String message;
private Object data;
private ApiResponse() {
}
private ApiResponse(Integer code, String message, Object data) { this.code = code; this.message = message; this.data = data; }
public static ApiResponse of(Integer code, String message, Object data) { return new ApiResponse(code, message, data); }
public static ApiResponse ofSuccess(Object data) { return ofStatus(Status.OK, data); }
public static ApiResponse ofMessage(String message) { return of(Status.OK.getCode(), message, null); }
public static ApiResponse ofStatus(Status status) { return ofStatus(status, null); }
public static ApiResponse ofStatus(Status status, Object data) { return of(status.getCode(), status.getMessage(), data); }
public static <T extends BaseException> ApiResponse ofException(T t, Object data) { return of(t.getCode(), t.getMessage(), data); }
public static <T extends BaseException> ApiResponse ofException(T t) { return ofException(t, null); } }
|
DemoExceptionHandler.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
@ControllerAdvice @Slf4j public class DemoExceptionHandler { private static final String DEFAULT_ERROR_VIEW = "error";
@ExceptionHandler(value = JsonException.class) @ResponseBody public ApiResponse jsonErrorHandler(JsonException exception) { log.error("【JsonException】:{}", exception.getMessage()); return ApiResponse.ofException(exception); }
@ExceptionHandler(value = PageException.class) public ModelAndView pageErrorHandler(PageException exception) { log.error("【DemoPageException】:{}", exception.getMessage()); ModelAndView view = new ModelAndView(); view.addObject("message", exception.getMessage()); view.setViewName(DEFAULT_ERROR_VIEW); return view; } }
|
error.html
位于 src/main/resources/template
目录下
1 2 3 4 5 6 7 8 9 10 11
| <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head lang="en"> <meta charset="UTF-8"/> <title>统一页面异常处理</title> </head> <body> <h1>统一页面异常处理</h1> <div th:text="${message}"></div> </body> </html>
|