From 73c54ea8627c4519c9b668a00a1b651676acc724 Mon Sep 17 00:00:00 2001 From: "Yangkai.Shen" <237497819@qq.com> Date: Sat, 20 Aug 2022 02:34:08 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E5=9F=BA=E7=A1=80=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E4=B9=8B=20=E7=BB=9F=E4=B8=80=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E6=8B=A6=E6=88=AA=20=E6=A1=88=E4=BE=8B=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- demo-base/demo-base-exception/README.md | 149 ++++++++++ demo-base/demo-base-exception/pom.xml | 63 +++++ .../handler/ExceptionHandlerApplication.java | 4 +- .../handler/controller/TestController.java | 14 +- .../handler/exception/JsonException.java | 9 +- .../handler/exception/PageException.java | 9 +- .../handler/handler/DemoExceptionHandler.java | 14 +- .../src/main/resources/application.yml | 0 .../src/main/resources/templates/error.html | 0 .../ExceptionHandlerApplicationTests.java | 13 + demo-base/pom.xml | 1 + demo-exception-handler/.gitignore | 25 -- demo-exception-handler/README.md | 260 ------------------ demo-exception-handler/pom.xml | 59 ---- .../exception/handler/constant/Status.java | 37 --- .../handler/exception/BaseException.java | 32 --- .../exception/handler/model/ApiResponse.java | 127 --------- ...tDemoExceptionHandlerApplicationTests.java | 16 -- pom.xml | 1 - 19 files changed, 252 insertions(+), 581 deletions(-) create mode 100644 demo-base/demo-base-exception/README.md create mode 100644 demo-base/demo-base-exception/pom.xml rename demo-exception-handler/src/main/java/com/xkcoding/exception/handler/SpringBootDemoExceptionHandlerApplication.java => demo-base/demo-base-exception/src/main/java/com/xkcoding/exception/handler/ExceptionHandlerApplication.java (70%) rename {demo-exception-handler => demo-base/demo-base-exception}/src/main/java/com/xkcoding/exception/handler/controller/TestController.java (65%) rename {demo-exception-handler => demo-base/demo-base-exception}/src/main/java/com/xkcoding/exception/handler/exception/JsonException.java (53%) rename {demo-exception-handler => demo-base/demo-base-exception}/src/main/java/com/xkcoding/exception/handler/exception/PageException.java (53%) rename {demo-exception-handler => demo-base/demo-base-exception}/src/main/java/com/xkcoding/exception/handler/handler/DemoExceptionHandler.java (81%) rename {demo-exception-handler => demo-base/demo-base-exception}/src/main/resources/application.yml (100%) rename {demo-exception-handler => demo-base/demo-base-exception}/src/main/resources/templates/error.html (100%) create mode 100644 demo-base/demo-base-exception/src/test/java/com/xkcoding/exception/handler/ExceptionHandlerApplicationTests.java delete mode 100644 demo-exception-handler/.gitignore delete mode 100644 demo-exception-handler/README.md delete mode 100644 demo-exception-handler/pom.xml delete mode 100644 demo-exception-handler/src/main/java/com/xkcoding/exception/handler/constant/Status.java delete mode 100644 demo-exception-handler/src/main/java/com/xkcoding/exception/handler/exception/BaseException.java delete mode 100644 demo-exception-handler/src/main/java/com/xkcoding/exception/handler/model/ApiResponse.java delete mode 100644 demo-exception-handler/src/test/java/com/xkcoding/exception/handler/SpringBootDemoExceptionHandlerApplicationTests.java diff --git a/demo-base/demo-base-exception/README.md b/demo-base/demo-base-exception/README.md new file mode 100644 index 0000000..f8a0e63 --- /dev/null +++ b/demo-base/demo-base-exception/README.md @@ -0,0 +1,149 @@ +## spring-boot-demo-exception-handler + +> 此 demo 演示了如何在Spring Boot中进行统一的异常处理,包括了两种方式的处理:第一种对常见API形式的接口进行异常处理,统一封装返回格式;第二种是对模板页面请求的异常处理,统一处理错误页面。 + +### 1.开发步骤 + +#### 1.1.添加依赖 + +```xml + + + com.xkcoding + common-tools + + + + org.springframework.boot + spring-boot-starter-web + + + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.projectlombok + lombok + true + + +``` + +#### 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 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 + + + + + 统一页面异常处理 + + +

统一页面异常处理

+
+ + +``` + +### 2.测试 + +#### 2.1.编写测试路由代码模拟异常 + +```java +@Controller +public class TestController { + + @GetMapping("/json") + @ResponseBody + public Response 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` ,同时观察控制台日志输出 diff --git a/demo-base/demo-base-exception/pom.xml b/demo-base/demo-base-exception/pom.xml new file mode 100644 index 0000000..daf0a47 --- /dev/null +++ b/demo-base/demo-base-exception/pom.xml @@ -0,0 +1,63 @@ + + + + com.xkcoding + demo-base + 1.0.0-SNAPSHOT + + + 4.0.0 + + demo-base-exception + 1.0.0-SNAPSHOT + jar + + demo-base-exception + Demo project for Spring Boot + + + 17 + + + + + com.xkcoding + common-tools + + + + org.springframework.boot + spring-boot-starter-web + + + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.projectlombok + lombok + true + + + + + demo-base-exception + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/demo-exception-handler/src/main/java/com/xkcoding/exception/handler/SpringBootDemoExceptionHandlerApplication.java b/demo-base/demo-base-exception/src/main/java/com/xkcoding/exception/handler/ExceptionHandlerApplication.java similarity index 70% rename from demo-exception-handler/src/main/java/com/xkcoding/exception/handler/SpringBootDemoExceptionHandlerApplication.java rename to demo-base/demo-base-exception/src/main/java/com/xkcoding/exception/handler/ExceptionHandlerApplication.java index 68c50c0..e29a349 100644 --- a/demo-exception-handler/src/main/java/com/xkcoding/exception/handler/SpringBootDemoExceptionHandlerApplication.java +++ b/demo-base/demo-base-exception/src/main/java/com/xkcoding/exception/handler/ExceptionHandlerApplication.java @@ -12,9 +12,9 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; * @date Created in 2018-10-02 20:49 */ @SpringBootApplication -public class SpringBootDemoExceptionHandlerApplication { +public class ExceptionHandlerApplication { public static void main(String[] args) { - SpringApplication.run(SpringBootDemoExceptionHandlerApplication.class, args); + SpringApplication.run(ExceptionHandlerApplication.class, args); } } diff --git a/demo-exception-handler/src/main/java/com/xkcoding/exception/handler/controller/TestController.java b/demo-base/demo-base-exception/src/main/java/com/xkcoding/exception/handler/controller/TestController.java similarity index 65% rename from demo-exception-handler/src/main/java/com/xkcoding/exception/handler/controller/TestController.java rename to demo-base/demo-base-exception/src/main/java/com/xkcoding/exception/handler/controller/TestController.java index 493e93c..e7f9b60 100644 --- a/demo-exception-handler/src/main/java/com/xkcoding/exception/handler/controller/TestController.java +++ b/demo-base/demo-base-exception/src/main/java/com/xkcoding/exception/handler/controller/TestController.java @@ -1,9 +1,9 @@ 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.PageException; -import com.xkcoding.exception.handler.model.ApiResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ResponseBody; @@ -11,23 +11,23 @@ import org.springframework.web.servlet.ModelAndView; /** *

- * 测试Controller + * 模拟测试路由 *

* * @author yangkai.shen - * @date Created in 2018-10-02 20:49 + * @date Created in 2022-08-20 02:11 */ @Controller public class TestController { @GetMapping("/json") @ResponseBody - public ApiResponse jsonException() { - throw new JsonException(Status.UNKNOWN_ERROR); + public Response jsonException() { + throw new JsonException(CommonStatus.SERVER_ERROR); } @GetMapping("/page") public ModelAndView pageException() { - throw new PageException(Status.UNKNOWN_ERROR); + throw new PageException(CommonStatus.SERVER_ERROR); } } diff --git a/demo-exception-handler/src/main/java/com/xkcoding/exception/handler/exception/JsonException.java b/demo-base/demo-base-exception/src/main/java/com/xkcoding/exception/handler/exception/JsonException.java similarity index 53% rename from demo-exception-handler/src/main/java/com/xkcoding/exception/handler/exception/JsonException.java rename to demo-base/demo-base-exception/src/main/java/com/xkcoding/exception/handler/exception/JsonException.java index fb71770..fa951de 100644 --- a/demo-exception-handler/src/main/java/com/xkcoding/exception/handler/exception/JsonException.java +++ b/demo-base/demo-base-exception/src/main/java/com/xkcoding/exception/handler/exception/JsonException.java @@ -1,6 +1,7 @@ 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; /** @@ -9,12 +10,12 @@ import lombok.Getter; *

* * @author yangkai.shen - * @date Created in 2018-10-02 21:18 + * @date Created in 2022-08-20 02:08 */ @Getter -public class JsonException extends BaseException { +public class JsonException extends CommonBizException { - public JsonException(Status status) { + public JsonException(IStatus status) { super(status); } diff --git a/demo-exception-handler/src/main/java/com/xkcoding/exception/handler/exception/PageException.java b/demo-base/demo-base-exception/src/main/java/com/xkcoding/exception/handler/exception/PageException.java similarity index 53% rename from demo-exception-handler/src/main/java/com/xkcoding/exception/handler/exception/PageException.java rename to demo-base/demo-base-exception/src/main/java/com/xkcoding/exception/handler/exception/PageException.java index 97c9ba7..be71ed1 100644 --- a/demo-exception-handler/src/main/java/com/xkcoding/exception/handler/exception/PageException.java +++ b/demo-base/demo-base-exception/src/main/java/com/xkcoding/exception/handler/exception/PageException.java @@ -1,6 +1,7 @@ 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; /** @@ -9,12 +10,12 @@ import lombok.Getter; *

* * @author yangkai.shen - * @date Created in 2018-10-02 21:18 + * @date Created in 2022-08-20 02:09 */ @Getter -public class PageException extends BaseException { +public class PageException extends CommonBizException { - public PageException(Status status) { + public PageException(IStatus status) { super(status); } diff --git a/demo-exception-handler/src/main/java/com/xkcoding/exception/handler/handler/DemoExceptionHandler.java b/demo-base/demo-base-exception/src/main/java/com/xkcoding/exception/handler/handler/DemoExceptionHandler.java similarity index 81% rename from demo-exception-handler/src/main/java/com/xkcoding/exception/handler/handler/DemoExceptionHandler.java rename to demo-base/demo-base-exception/src/main/java/com/xkcoding/exception/handler/handler/DemoExceptionHandler.java index 32eacf8..a3c7e79 100644 --- a/demo-exception-handler/src/main/java/com/xkcoding/exception/handler/handler/DemoExceptionHandler.java +++ b/demo-base/demo-base-exception/src/main/java/com/xkcoding/exception/handler/handler/DemoExceptionHandler.java @@ -1,8 +1,8 @@ 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.PageException; -import com.xkcoding.exception.handler.model.ApiResponse; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; @@ -11,14 +11,14 @@ import org.springframework.web.servlet.ModelAndView; /** *

- * 统一异常处理 + * 异常拦截 *

* * @author yangkai.shen - * @date Created in 2018-10-02 21:26 + * @date Created in 2022-08-20 02:11 */ -@ControllerAdvice @Slf4j +@ControllerAdvice public class DemoExceptionHandler { private static final String DEFAULT_ERROR_VIEW = "error"; @@ -30,9 +30,9 @@ public class DemoExceptionHandler { */ @ExceptionHandler(value = JsonException.class) @ResponseBody - public ApiResponse jsonErrorHandler(JsonException exception) { + public Response jsonErrorHandler(JsonException exception) { log.error("【JsonException】:{}", exception.getMessage()); - return ApiResponse.ofException(exception); + return Response.ofError(exception); } /** @@ -43,7 +43,7 @@ public class DemoExceptionHandler { */ @ExceptionHandler(value = PageException.class) public ModelAndView pageErrorHandler(PageException exception) { - log.error("【DemoPageException】:{}", exception.getMessage()); + log.error("【PageException】:{}", exception.getMessage()); ModelAndView view = new ModelAndView(); view.addObject("message", exception.getMessage()); view.setViewName(DEFAULT_ERROR_VIEW); diff --git a/demo-exception-handler/src/main/resources/application.yml b/demo-base/demo-base-exception/src/main/resources/application.yml similarity index 100% rename from demo-exception-handler/src/main/resources/application.yml rename to demo-base/demo-base-exception/src/main/resources/application.yml diff --git a/demo-exception-handler/src/main/resources/templates/error.html b/demo-base/demo-base-exception/src/main/resources/templates/error.html similarity index 100% rename from demo-exception-handler/src/main/resources/templates/error.html rename to demo-base/demo-base-exception/src/main/resources/templates/error.html diff --git a/demo-base/demo-base-exception/src/test/java/com/xkcoding/exception/handler/ExceptionHandlerApplicationTests.java b/demo-base/demo-base-exception/src/test/java/com/xkcoding/exception/handler/ExceptionHandlerApplicationTests.java new file mode 100644 index 0000000..692a1bf --- /dev/null +++ b/demo-base/demo-base-exception/src/test/java/com/xkcoding/exception/handler/ExceptionHandlerApplicationTests.java @@ -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() { + } + +} diff --git a/demo-base/pom.xml b/demo-base/pom.xml index 93eaa4c..4df90f9 100644 --- a/demo-base/pom.xml +++ b/demo-base/pom.xml @@ -22,6 +22,7 @@ demo-base-helloworld demo-base-properties demo-base-async + demo-base-exception diff --git a/demo-exception-handler/.gitignore b/demo-exception-handler/.gitignore deleted file mode 100644 index 82eca33..0000000 --- a/demo-exception-handler/.gitignore +++ /dev/null @@ -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/ \ No newline at end of file diff --git a/demo-exception-handler/README.md b/demo-exception-handler/README.md deleted file mode 100644 index 040276f..0000000 --- a/demo-exception-handler/README.md +++ /dev/null @@ -1,260 +0,0 @@ -# spring-boot-demo-exception-handler - -> 此 demo 演示了如何在Spring Boot中进行统一的异常处理,包括了两种方式的处理:第一种对常见API形式的接口进行异常处理,统一封装返回格式;第二种是对模板页面请求的异常处理,统一处理错误页面。 - -## pom.xml - -```xml - - - 4.0.0 - - spring-boot-demo-exception-handler - 1.0.0-SNAPSHOT - jar - - spring-boot-demo-exception-handler - Demo project for Spring Boot - - - com.xkcoding - spring-boot-demo - 1.0.0-SNAPSHOT - - - - UTF-8 - UTF-8 - 1.8 - - - - - org.springframework.boot - spring-boot-starter-thymeleaf - - - - org.springframework.boot - spring-boot-starter-web - - - - org.springframework.boot - spring-boot-starter-test - test - - - - org.projectlombok - lombok - true - - - - - spring-boot-demo-exception-handler - - - org.springframework.boot - spring-boot-maven-plugin - - - - - -``` - -## ApiResponse.java - -> 统一的API格式返回封装,里面涉及到的 `BaseException` 和`Status` 这两个类,具体代码见 demo。 - -```java -/** - *

- * 通用的 API 接口封装 - *

- * - * @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 {@link BaseException} 的子类 - * @return ApiResponse - */ - public static ApiResponse ofException(T t, Object data) { - return of(t.getCode(), t.getMessage(), data); - } - - /** - * 构造一个异常且带数据的API返回 - * - * @param t 异常 - * @param {@link BaseException} 的子类 - * @return ApiResponse - */ - public static ApiResponse ofException(T t) { - return ofException(t, null); - } -} -``` - -## DemoExceptionHandler.java - -```java -/** - *

- * 统一异常处理 - *

- * - * @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 - - - - - 统一页面异常处理 - - -

统一页面异常处理

-
- - -``` - diff --git a/demo-exception-handler/pom.xml b/demo-exception-handler/pom.xml deleted file mode 100644 index 7e543f1..0000000 --- a/demo-exception-handler/pom.xml +++ /dev/null @@ -1,59 +0,0 @@ - - - 4.0.0 - - demo-exception-handler - 1.0.0-SNAPSHOT - jar - - demo-exception-handler - Demo project for Spring Boot - - - com.xkcoding - spring-boot-demo - 1.0.0-SNAPSHOT - - - - UTF-8 - UTF-8 - 1.8 - - - - - org.springframework.boot - spring-boot-starter-thymeleaf - - - - org.springframework.boot - spring-boot-starter-web - - - - org.springframework.boot - spring-boot-starter-test - test - - - - org.projectlombok - lombok - true - - - - - demo-exception-handler - - - org.springframework.boot - spring-boot-maven-plugin - - - - - diff --git a/demo-exception-handler/src/main/java/com/xkcoding/exception/handler/constant/Status.java b/demo-exception-handler/src/main/java/com/xkcoding/exception/handler/constant/Status.java deleted file mode 100644 index 3f0eb18..0000000 --- a/demo-exception-handler/src/main/java/com/xkcoding/exception/handler/constant/Status.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.xkcoding.exception.handler.constant; - -import lombok.Getter; - -/** - *

- * 状态码封装 - *

- * - * @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; - } -} diff --git a/demo-exception-handler/src/main/java/com/xkcoding/exception/handler/exception/BaseException.java b/demo-exception-handler/src/main/java/com/xkcoding/exception/handler/exception/BaseException.java deleted file mode 100644 index 2d42003..0000000 --- a/demo-exception-handler/src/main/java/com/xkcoding/exception/handler/exception/BaseException.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.xkcoding.exception.handler.exception; - -import com.xkcoding.exception.handler.constant.Status; -import lombok.Data; -import lombok.EqualsAndHashCode; - -/** - *

- * 异常基类 - *

- * - * @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; - } -} diff --git a/demo-exception-handler/src/main/java/com/xkcoding/exception/handler/model/ApiResponse.java b/demo-exception-handler/src/main/java/com/xkcoding/exception/handler/model/ApiResponse.java deleted file mode 100644 index 8c5fa71..0000000 --- a/demo-exception-handler/src/main/java/com/xkcoding/exception/handler/model/ApiResponse.java +++ /dev/null @@ -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; - -/** - *

- * 通用的 API 接口封装 - *

- * - * @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 {@link BaseException} 的子类 - * @return ApiResponse - */ - public static ApiResponse ofException(T t, Object data) { - return of(t.getCode(), t.getMessage(), data); - } - - /** - * 构造一个异常且带数据的API返回 - * - * @param t 异常 - * @param {@link BaseException} 的子类 - * @return ApiResponse - */ - public static ApiResponse ofException(T t) { - return ofException(t, null); - } -} diff --git a/demo-exception-handler/src/test/java/com/xkcoding/exception/handler/SpringBootDemoExceptionHandlerApplicationTests.java b/demo-exception-handler/src/test/java/com/xkcoding/exception/handler/SpringBootDemoExceptionHandlerApplicationTests.java deleted file mode 100644 index 489e1b2..0000000 --- a/demo-exception-handler/src/test/java/com/xkcoding/exception/handler/SpringBootDemoExceptionHandlerApplicationTests.java +++ /dev/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() { - } - -} diff --git a/pom.xml b/pom.xml index 9871805..e0059ad 100644 --- a/pom.xml +++ b/pom.xml @@ -30,7 +30,6 @@ demo-others -