@@ -0,0 +1,149 @@ | |||||
## spring-boot-demo-exception-handler | |||||
> 此 demo 演示了如何在Spring Boot中进行统一的异常处理,包括了两种方式的处理:第一种对常见API形式的接口进行异常处理,统一封装返回格式;第二种是对模板页面请求的异常处理,统一处理错误页面。 | |||||
### 1.开发步骤 | |||||
#### 1.1.添加依赖 | |||||
```xml | |||||
<dependencies> | |||||
<dependency> | |||||
<groupId>com.xkcoding</groupId> | |||||
<artifactId>common-tools</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-thymeleaf</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> | |||||
``` | |||||
#### 1.2.构造JSON异常和页面跳转异常 | |||||
```java | |||||
// JSON 异常 | |||||
@Getter | |||||
public class JsonException extends CommonBizException { | |||||
public JsonException(IStatus status) { | |||||
super(status); | |||||
} | |||||
public JsonException(Integer code, String message) { | |||||
super(code, message); | |||||
} | |||||
} | |||||
// 页面异常 | |||||
@Getter | |||||
public class PageException extends CommonBizException { | |||||
public PageException(IStatus status) { | |||||
super(status); | |||||
} | |||||
public PageException(Integer code, String message) { | |||||
super(code, message); | |||||
} | |||||
} | |||||
``` | |||||
#### 1.3.异常拦截 | |||||
```java | |||||
@Slf4j | |||||
@ControllerAdvice | |||||
public class DemoExceptionHandler { | |||||
private static final String DEFAULT_ERROR_VIEW = "error"; | |||||
/** | |||||
* 统一 json 异常处理 | |||||
* | |||||
* @param exception JsonException | |||||
* @return 统一返回 json 格式 | |||||
*/ | |||||
@ExceptionHandler(value = JsonException.class) | |||||
@ResponseBody | |||||
public Response<Void> jsonErrorHandler(JsonException exception) { | |||||
log.error("【JsonException】:{}", exception.getMessage()); | |||||
return Response.ofError(exception); | |||||
} | |||||
/** | |||||
* 统一 页面 异常处理 | |||||
* | |||||
* @param exception PageException | |||||
* @return 统一跳转到异常页面 | |||||
*/ | |||||
@ExceptionHandler(value = PageException.class) | |||||
public ModelAndView pageErrorHandler(PageException exception) { | |||||
log.error("【PageException】:{}", exception.getMessage()); | |||||
ModelAndView view = new ModelAndView(); | |||||
view.addObject("message", exception.getMessage()); | |||||
view.setViewName(DEFAULT_ERROR_VIEW); | |||||
return view; | |||||
} | |||||
} | |||||
``` | |||||
#### 1.4.编写统一错误页面 | |||||
> 位于 `src/main/resources/template` 目录下 | |||||
```html | |||||
<!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> | |||||
``` | |||||
### 2.测试 | |||||
#### 2.1.编写测试路由代码模拟异常 | |||||
```java | |||||
@Controller | |||||
public class TestController { | |||||
@GetMapping("/json") | |||||
@ResponseBody | |||||
public Response<Void> jsonException() { | |||||
throw new JsonException(CommonStatus.SERVER_ERROR); | |||||
} | |||||
@GetMapping("/page") | |||||
public ModelAndView pageException() { | |||||
throw new PageException(CommonStatus.SERVER_ERROR); | |||||
} | |||||
} | |||||
``` | |||||
启动 `ExceptionHandlerApplication` | |||||
- 测试API形式的异常拦截返回,浏览器进入 `http://localhost:8080/demo/json` ,同时观察控制台日志输出 | |||||
- 测试统一错误页面的异常拦截返回,浏览器进入 `http://localhost:8080/demo/page` ,同时观察控制台日志输出 |
@@ -0,0 +1,63 @@ | |||||
<?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"> | |||||
<parent> | |||||
<groupId>com.xkcoding</groupId> | |||||
<artifactId>demo-base</artifactId> | |||||
<version>1.0.0-SNAPSHOT</version> | |||||
</parent> | |||||
<modelVersion>4.0.0</modelVersion> | |||||
<artifactId>demo-base-exception</artifactId> | |||||
<version>1.0.0-SNAPSHOT</version> | |||||
<packaging>jar</packaging> | |||||
<name>demo-base-exception</name> | |||||
<description>Demo project for Spring Boot</description> | |||||
<properties> | |||||
<java.version>17</java.version> | |||||
</properties> | |||||
<dependencies> | |||||
<dependency> | |||||
<groupId>com.xkcoding</groupId> | |||||
<artifactId>common-tools</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-thymeleaf</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>demo-base-exception</finalName> | |||||
<plugins> | |||||
<plugin> | |||||
<groupId>org.springframework.boot</groupId> | |||||
<artifactId>spring-boot-maven-plugin</artifactId> | |||||
</plugin> | |||||
</plugins> | |||||
</build> | |||||
</project> |
@@ -12,9 +12,9 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; | |||||
* @date Created in 2018-10-02 20:49 | * @date Created in 2018-10-02 20:49 | ||||
*/ | */ | ||||
@SpringBootApplication | @SpringBootApplication | ||||
public class SpringBootDemoExceptionHandlerApplication { | |||||
public class ExceptionHandlerApplication { | |||||
public static void main(String[] args) { | public static void main(String[] args) { | ||||
SpringApplication.run(SpringBootDemoExceptionHandlerApplication.class, args); | |||||
SpringApplication.run(ExceptionHandlerApplication.class, args); | |||||
} | } | ||||
} | } |
@@ -1,9 +1,9 @@ | |||||
package com.xkcoding.exception.handler.controller; | package com.xkcoding.exception.handler.controller; | ||||
import com.xkcoding.exception.handler.constant.Status; | |||||
import com.xkcoding.common.enums.CommonStatus; | |||||
import com.xkcoding.common.model.viewmodel.Response; | |||||
import com.xkcoding.exception.handler.exception.JsonException; | import com.xkcoding.exception.handler.exception.JsonException; | ||||
import com.xkcoding.exception.handler.exception.PageException; | import com.xkcoding.exception.handler.exception.PageException; | ||||
import com.xkcoding.exception.handler.model.ApiResponse; | |||||
import org.springframework.stereotype.Controller; | import org.springframework.stereotype.Controller; | ||||
import org.springframework.web.bind.annotation.GetMapping; | import org.springframework.web.bind.annotation.GetMapping; | ||||
import org.springframework.web.bind.annotation.ResponseBody; | import org.springframework.web.bind.annotation.ResponseBody; | ||||
@@ -11,23 +11,23 @@ import org.springframework.web.servlet.ModelAndView; | |||||
/** | /** | ||||
* <p> | * <p> | ||||
* 测试Controller | |||||
* 模拟测试路由 | |||||
* </p> | * </p> | ||||
* | * | ||||
* @author yangkai.shen | * @author yangkai.shen | ||||
* @date Created in 2018-10-02 20:49 | |||||
* @date Created in 2022-08-20 02:11 | |||||
*/ | */ | ||||
@Controller | @Controller | ||||
public class TestController { | public class TestController { | ||||
@GetMapping("/json") | @GetMapping("/json") | ||||
@ResponseBody | @ResponseBody | ||||
public ApiResponse jsonException() { | |||||
throw new JsonException(Status.UNKNOWN_ERROR); | |||||
public Response<Void> jsonException() { | |||||
throw new JsonException(CommonStatus.SERVER_ERROR); | |||||
} | } | ||||
@GetMapping("/page") | @GetMapping("/page") | ||||
public ModelAndView pageException() { | public ModelAndView pageException() { | ||||
throw new PageException(Status.UNKNOWN_ERROR); | |||||
throw new PageException(CommonStatus.SERVER_ERROR); | |||||
} | } | ||||
} | } |
@@ -1,6 +1,7 @@ | |||||
package com.xkcoding.exception.handler.exception; | package com.xkcoding.exception.handler.exception; | ||||
import com.xkcoding.exception.handler.constant.Status; | |||||
import com.xkcoding.common.enums.base.IStatus; | |||||
import com.xkcoding.common.exception.CommonBizException; | |||||
import lombok.Getter; | import lombok.Getter; | ||||
/** | /** | ||||
@@ -9,12 +10,12 @@ import lombok.Getter; | |||||
* </p> | * </p> | ||||
* | * | ||||
* @author yangkai.shen | * @author yangkai.shen | ||||
* @date Created in 2018-10-02 21:18 | |||||
* @date Created in 2022-08-20 02:08 | |||||
*/ | */ | ||||
@Getter | @Getter | ||||
public class JsonException extends BaseException { | |||||
public class JsonException extends CommonBizException { | |||||
public JsonException(Status status) { | |||||
public JsonException(IStatus status) { | |||||
super(status); | super(status); | ||||
} | } | ||||
@@ -1,6 +1,7 @@ | |||||
package com.xkcoding.exception.handler.exception; | package com.xkcoding.exception.handler.exception; | ||||
import com.xkcoding.exception.handler.constant.Status; | |||||
import com.xkcoding.common.enums.base.IStatus; | |||||
import com.xkcoding.common.exception.CommonBizException; | |||||
import lombok.Getter; | import lombok.Getter; | ||||
/** | /** | ||||
@@ -9,12 +10,12 @@ import lombok.Getter; | |||||
* </p> | * </p> | ||||
* | * | ||||
* @author yangkai.shen | * @author yangkai.shen | ||||
* @date Created in 2018-10-02 21:18 | |||||
* @date Created in 2022-08-20 02:09 | |||||
*/ | */ | ||||
@Getter | @Getter | ||||
public class PageException extends BaseException { | |||||
public class PageException extends CommonBizException { | |||||
public PageException(Status status) { | |||||
public PageException(IStatus status) { | |||||
super(status); | super(status); | ||||
} | } | ||||
@@ -1,8 +1,8 @@ | |||||
package com.xkcoding.exception.handler.handler; | package com.xkcoding.exception.handler.handler; | ||||
import com.xkcoding.common.model.viewmodel.Response; | |||||
import com.xkcoding.exception.handler.exception.JsonException; | import com.xkcoding.exception.handler.exception.JsonException; | ||||
import com.xkcoding.exception.handler.exception.PageException; | import com.xkcoding.exception.handler.exception.PageException; | ||||
import com.xkcoding.exception.handler.model.ApiResponse; | |||||
import lombok.extern.slf4j.Slf4j; | import lombok.extern.slf4j.Slf4j; | ||||
import org.springframework.web.bind.annotation.ControllerAdvice; | import org.springframework.web.bind.annotation.ControllerAdvice; | ||||
import org.springframework.web.bind.annotation.ExceptionHandler; | import org.springframework.web.bind.annotation.ExceptionHandler; | ||||
@@ -11,14 +11,14 @@ import org.springframework.web.servlet.ModelAndView; | |||||
/** | /** | ||||
* <p> | * <p> | ||||
* 统一异常处理 | |||||
* 异常拦截 | |||||
* </p> | * </p> | ||||
* | * | ||||
* @author yangkai.shen | * @author yangkai.shen | ||||
* @date Created in 2018-10-02 21:26 | |||||
* @date Created in 2022-08-20 02:11 | |||||
*/ | */ | ||||
@ControllerAdvice | |||||
@Slf4j | @Slf4j | ||||
@ControllerAdvice | |||||
public class DemoExceptionHandler { | public class DemoExceptionHandler { | ||||
private static final String DEFAULT_ERROR_VIEW = "error"; | private static final String DEFAULT_ERROR_VIEW = "error"; | ||||
@@ -30,9 +30,9 @@ public class DemoExceptionHandler { | |||||
*/ | */ | ||||
@ExceptionHandler(value = JsonException.class) | @ExceptionHandler(value = JsonException.class) | ||||
@ResponseBody | @ResponseBody | ||||
public ApiResponse jsonErrorHandler(JsonException exception) { | |||||
public Response<Void> jsonErrorHandler(JsonException exception) { | |||||
log.error("【JsonException】:{}", exception.getMessage()); | log.error("【JsonException】:{}", exception.getMessage()); | ||||
return ApiResponse.ofException(exception); | |||||
return Response.ofError(exception); | |||||
} | } | ||||
/** | /** | ||||
@@ -43,7 +43,7 @@ public class DemoExceptionHandler { | |||||
*/ | */ | ||||
@ExceptionHandler(value = PageException.class) | @ExceptionHandler(value = PageException.class) | ||||
public ModelAndView pageErrorHandler(PageException exception) { | public ModelAndView pageErrorHandler(PageException exception) { | ||||
log.error("【DemoPageException】:{}", exception.getMessage()); | |||||
log.error("【PageException】:{}", exception.getMessage()); | |||||
ModelAndView view = new ModelAndView(); | ModelAndView view = new ModelAndView(); | ||||
view.addObject("message", exception.getMessage()); | view.addObject("message", exception.getMessage()); | ||||
view.setViewName(DEFAULT_ERROR_VIEW); | view.setViewName(DEFAULT_ERROR_VIEW); |
@@ -0,0 +1,13 @@ | |||||
package com.xkcoding.exception.handler; | |||||
import org.junit.jupiter.api.Test; | |||||
import org.springframework.boot.test.context.SpringBootTest; | |||||
@SpringBootTest | |||||
class ExceptionHandlerApplicationTests { | |||||
@Test | |||||
void contextLoads() { | |||||
} | |||||
} |
@@ -22,6 +22,7 @@ | |||||
<module>demo-base-helloworld</module> | <module>demo-base-helloworld</module> | ||||
<module>demo-base-properties</module> | <module>demo-base-properties</module> | ||||
<module>demo-base-async</module> | <module>demo-base-async</module> | ||||
<module>demo-base-exception</module> | |||||
</modules> | </modules> | ||||
</project> | </project> |
@@ -1,25 +0,0 @@ | |||||
/target/ | |||||
!.mvn/wrapper/maven-wrapper.jar | |||||
### STS ### | |||||
.apt_generated | |||||
.classpath | |||||
.factorypath | |||||
.project | |||||
.settings | |||||
.springBeans | |||||
.sts4-cache | |||||
### IntelliJ IDEA ### | |||||
.idea | |||||
*.iws | |||||
*.iml | |||||
*.ipr | |||||
### NetBeans ### | |||||
/nbproject/private/ | |||||
/build/ | |||||
/nbbuild/ | |||||
/dist/ | |||||
/nbdist/ | |||||
/.nb-gradle/ |
@@ -1,260 +0,0 @@ | |||||
# spring-boot-demo-exception-handler | |||||
> 此 demo 演示了如何在Spring Boot中进行统一的异常处理,包括了两种方式的处理:第一种对常见API形式的接口进行异常处理,统一封装返回格式;第二种是对模板页面请求的异常处理,统一处理错误页面。 | |||||
## pom.xml | |||||
```xml | |||||
<?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> | |||||
``` | |||||
## ApiResponse.java | |||||
> 统一的API格式返回封装,里面涉及到的 `BaseException` 和`Status` 这两个类,具体代码见 demo。 | |||||
```java | |||||
/** | |||||
* <p> | |||||
* 通用的 API 接口封装 | |||||
* </p> | |||||
* | |||||
* @author yangkai.shen | |||||
* @date Created in 2018-10-02 20:57 | |||||
*/ | |||||
@Data | |||||
public class ApiResponse { | |||||
/** | |||||
* 状态码 | |||||
*/ | |||||
private Integer code; | |||||
/** | |||||
* 返回内容 | |||||
*/ | |||||
private String message; | |||||
/** | |||||
* 返回数据 | |||||
*/ | |||||
private Object data; | |||||
/** | |||||
* 无参构造函数 | |||||
*/ | |||||
private ApiResponse() { | |||||
} | |||||
/** | |||||
* 全参构造函数 | |||||
* | |||||
* @param code 状态码 | |||||
* @param message 返回内容 | |||||
* @param data 返回数据 | |||||
*/ | |||||
private ApiResponse(Integer code, String message, Object data) { | |||||
this.code = code; | |||||
this.message = message; | |||||
this.data = data; | |||||
} | |||||
/** | |||||
* 构造一个自定义的API返回 | |||||
* | |||||
* @param code 状态码 | |||||
* @param message 返回内容 | |||||
* @param data 返回数据 | |||||
* @return ApiResponse | |||||
*/ | |||||
public static ApiResponse of(Integer code, String message, Object data) { | |||||
return new ApiResponse(code, message, data); | |||||
} | |||||
/** | |||||
* 构造一个成功且带数据的API返回 | |||||
* | |||||
* @param data 返回数据 | |||||
* @return ApiResponse | |||||
*/ | |||||
public static ApiResponse ofSuccess(Object data) { | |||||
return ofStatus(Status.OK, data); | |||||
} | |||||
/** | |||||
* 构造一个成功且自定义消息的API返回 | |||||
* | |||||
* @param message 返回内容 | |||||
* @return ApiResponse | |||||
*/ | |||||
public static ApiResponse ofMessage(String message) { | |||||
return of(Status.OK.getCode(), message, null); | |||||
} | |||||
/** | |||||
* 构造一个有状态的API返回 | |||||
* | |||||
* @param status 状态 {@link Status} | |||||
* @return ApiResponse | |||||
*/ | |||||
public static ApiResponse ofStatus(Status status) { | |||||
return ofStatus(status, null); | |||||
} | |||||
/** | |||||
* 构造一个有状态且带数据的API返回 | |||||
* | |||||
* @param status 状态 {@link Status} | |||||
* @param data 返回数据 | |||||
* @return ApiResponse | |||||
*/ | |||||
public static ApiResponse ofStatus(Status status, Object data) { | |||||
return of(status.getCode(), status.getMessage(), data); | |||||
} | |||||
/** | |||||
* 构造一个异常且带数据的API返回 | |||||
* | |||||
* @param t 异常 | |||||
* @param data 返回数据 | |||||
* @param <T> {@link BaseException} 的子类 | |||||
* @return ApiResponse | |||||
*/ | |||||
public static <T extends BaseException> ApiResponse ofException(T t, Object data) { | |||||
return of(t.getCode(), t.getMessage(), data); | |||||
} | |||||
/** | |||||
* 构造一个异常且带数据的API返回 | |||||
* | |||||
* @param t 异常 | |||||
* @param <T> {@link BaseException} 的子类 | |||||
* @return ApiResponse | |||||
*/ | |||||
public static <T extends BaseException> ApiResponse ofException(T t) { | |||||
return ofException(t, null); | |||||
} | |||||
} | |||||
``` | |||||
## DemoExceptionHandler.java | |||||
```java | |||||
/** | |||||
* <p> | |||||
* 统一异常处理 | |||||
* </p> | |||||
* | |||||
* @author yangkai.shen | |||||
* @date Created in 2018-10-02 21:26 | |||||
*/ | |||||
@ControllerAdvice | |||||
@Slf4j | |||||
public class DemoExceptionHandler { | |||||
private static final String DEFAULT_ERROR_VIEW = "error"; | |||||
/** | |||||
* 统一 json 异常处理 | |||||
* | |||||
* @param exception JsonException | |||||
* @return 统一返回 json 格式 | |||||
*/ | |||||
@ExceptionHandler(value = JsonException.class) | |||||
@ResponseBody | |||||
public ApiResponse jsonErrorHandler(JsonException exception) { | |||||
log.error("【JsonException】:{}", exception.getMessage()); | |||||
return ApiResponse.ofException(exception); | |||||
} | |||||
/** | |||||
* 统一 页面 异常处理 | |||||
* | |||||
* @param exception PageException | |||||
* @return 统一跳转到异常页面 | |||||
*/ | |||||
@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` 目录下 | |||||
```html | |||||
<!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> | |||||
``` | |||||
@@ -1,59 +0,0 @@ | |||||
<?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>demo-exception-handler</artifactId> | |||||
<version>1.0.0-SNAPSHOT</version> | |||||
<packaging>jar</packaging> | |||||
<name>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>demo-exception-handler</finalName> | |||||
<plugins> | |||||
<plugin> | |||||
<groupId>org.springframework.boot</groupId> | |||||
<artifactId>spring-boot-maven-plugin</artifactId> | |||||
</plugin> | |||||
</plugins> | |||||
</build> | |||||
</project> |
@@ -1,37 +0,0 @@ | |||||
package com.xkcoding.exception.handler.constant; | |||||
import lombok.Getter; | |||||
/** | |||||
* <p> | |||||
* 状态码封装 | |||||
* </p> | |||||
* | |||||
* @author yangkai.shen | |||||
* @date Created in 2018-10-02 21:02 | |||||
*/ | |||||
@Getter | |||||
public enum Status { | |||||
/** | |||||
* 操作成功 | |||||
*/ | |||||
OK(200, "操作成功"), | |||||
/** | |||||
* 未知异常 | |||||
*/ | |||||
UNKNOWN_ERROR(500, "服务器出错啦"); | |||||
/** | |||||
* 状态码 | |||||
*/ | |||||
private Integer code; | |||||
/** | |||||
* 内容 | |||||
*/ | |||||
private String message; | |||||
Status(Integer code, String message) { | |||||
this.code = code; | |||||
this.message = message; | |||||
} | |||||
} |
@@ -1,32 +0,0 @@ | |||||
package com.xkcoding.exception.handler.exception; | |||||
import com.xkcoding.exception.handler.constant.Status; | |||||
import lombok.Data; | |||||
import lombok.EqualsAndHashCode; | |||||
/** | |||||
* <p> | |||||
* 异常基类 | |||||
* </p> | |||||
* | |||||
* @author yangkai.shen | |||||
* @date Created in 2018-10-02 21:31 | |||||
*/ | |||||
@Data | |||||
@EqualsAndHashCode(callSuper = true) | |||||
public class BaseException extends RuntimeException { | |||||
private Integer code; | |||||
private String message; | |||||
public BaseException(Status status) { | |||||
super(status.getMessage()); | |||||
this.code = status.getCode(); | |||||
this.message = status.getMessage(); | |||||
} | |||||
public BaseException(Integer code, String message) { | |||||
super(message); | |||||
this.code = code; | |||||
this.message = message; | |||||
} | |||||
} |
@@ -1,127 +0,0 @@ | |||||
package com.xkcoding.exception.handler.model; | |||||
import com.xkcoding.exception.handler.constant.Status; | |||||
import com.xkcoding.exception.handler.exception.BaseException; | |||||
import lombok.Data; | |||||
/** | |||||
* <p> | |||||
* 通用的 API 接口封装 | |||||
* </p> | |||||
* | |||||
* @author yangkai.shen | |||||
* @date Created in 2018-10-02 20:57 | |||||
*/ | |||||
@Data | |||||
public class ApiResponse { | |||||
/** | |||||
* 状态码 | |||||
*/ | |||||
private Integer code; | |||||
/** | |||||
* 返回内容 | |||||
*/ | |||||
private String message; | |||||
/** | |||||
* 返回数据 | |||||
*/ | |||||
private Object data; | |||||
/** | |||||
* 无参构造函数 | |||||
*/ | |||||
private ApiResponse() { | |||||
} | |||||
/** | |||||
* 全参构造函数 | |||||
* | |||||
* @param code 状态码 | |||||
* @param message 返回内容 | |||||
* @param data 返回数据 | |||||
*/ | |||||
private ApiResponse(Integer code, String message, Object data) { | |||||
this.code = code; | |||||
this.message = message; | |||||
this.data = data; | |||||
} | |||||
/** | |||||
* 构造一个自定义的API返回 | |||||
* | |||||
* @param code 状态码 | |||||
* @param message 返回内容 | |||||
* @param data 返回数据 | |||||
* @return ApiResponse | |||||
*/ | |||||
public static ApiResponse of(Integer code, String message, Object data) { | |||||
return new ApiResponse(code, message, data); | |||||
} | |||||
/** | |||||
* 构造一个成功且带数据的API返回 | |||||
* | |||||
* @param data 返回数据 | |||||
* @return ApiResponse | |||||
*/ | |||||
public static ApiResponse ofSuccess(Object data) { | |||||
return ofStatus(Status.OK, data); | |||||
} | |||||
/** | |||||
* 构造一个成功且自定义消息的API返回 | |||||
* | |||||
* @param message 返回内容 | |||||
* @return ApiResponse | |||||
*/ | |||||
public static ApiResponse ofMessage(String message) { | |||||
return of(Status.OK.getCode(), message, null); | |||||
} | |||||
/** | |||||
* 构造一个有状态的API返回 | |||||
* | |||||
* @param status 状态 {@link Status} | |||||
* @return ApiResponse | |||||
*/ | |||||
public static ApiResponse ofStatus(Status status) { | |||||
return ofStatus(status, null); | |||||
} | |||||
/** | |||||
* 构造一个有状态且带数据的API返回 | |||||
* | |||||
* @param status 状态 {@link Status} | |||||
* @param data 返回数据 | |||||
* @return ApiResponse | |||||
*/ | |||||
public static ApiResponse ofStatus(Status status, Object data) { | |||||
return of(status.getCode(), status.getMessage(), data); | |||||
} | |||||
/** | |||||
* 构造一个异常且带数据的API返回 | |||||
* | |||||
* @param t 异常 | |||||
* @param data 返回数据 | |||||
* @param <T> {@link BaseException} 的子类 | |||||
* @return ApiResponse | |||||
*/ | |||||
public static <T extends BaseException> ApiResponse ofException(T t, Object data) { | |||||
return of(t.getCode(), t.getMessage(), data); | |||||
} | |||||
/** | |||||
* 构造一个异常且带数据的API返回 | |||||
* | |||||
* @param t 异常 | |||||
* @param <T> {@link BaseException} 的子类 | |||||
* @return ApiResponse | |||||
*/ | |||||
public static <T extends BaseException> ApiResponse ofException(T t) { | |||||
return ofException(t, null); | |||||
} | |||||
} |
@@ -1,16 +0,0 @@ | |||||
package com.xkcoding.exception.handler; | |||||
import org.junit.Test; | |||||
import org.junit.runner.RunWith; | |||||
import org.springframework.boot.test.context.SpringBootTest; | |||||
import org.springframework.test.context.junit4.SpringRunner; | |||||
@RunWith(SpringRunner.class) | |||||
@SpringBootTest | |||||
public class SpringBootDemoExceptionHandlerApplicationTests { | |||||
@Test | |||||
public void contextLoads() { | |||||
} | |||||
} |
@@ -30,7 +30,6 @@ | |||||
<module>demo-others</module> | <module>demo-others</module> | ||||
<!-- <module>demo-logback</module>--> | <!-- <module>demo-logback</module>--> | ||||
<!-- <module>demo-log-aop</module>--> | <!-- <module>demo-log-aop</module>--> | ||||
<!-- <module>demo-exception-handler</module>--> | |||||
<!-- <module>demo-template-freemarker</module>--> | <!-- <module>demo-template-freemarker</module>--> | ||||
<!-- <module>demo-template-thymeleaf</module>--> | <!-- <module>demo-template-thymeleaf</module>--> | ||||
<!-- <module>demo-template-beetl</module>--> | <!-- <module>demo-template-beetl</module>--> | ||||