diff --git a/spring-boot/exceptionTest/.gitignore b/spring-boot/exceptionTest/.gitignore new file mode 100644 index 0000000..549e00a --- /dev/null +++ b/spring-boot/exceptionTest/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/spring-boot/exceptionTest/pom.xml b/spring-boot/exceptionTest/pom.xml new file mode 100644 index 0000000..6b13d70 --- /dev/null +++ b/spring-boot/exceptionTest/pom.xml @@ -0,0 +1,50 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.5.5 + + + com.fanxb + exceptionTest + 0.0.1-SNAPSHOT + exceptionTest + Demo project for Spring Boot + + 11 + + + + org.springframework.boot + spring-boot-starter-web + + + + org.projectlombok + lombok + true + + + + + + + org.springframework.boot + spring-boot-maven-plugin + ${project.parent.version} + + + + org.projectlombok + lombok + + + + + + + + diff --git a/spring-boot/exceptionTest/src/main/java/com/fanxb/exceptiontest/ExceptionTestApplication.java b/spring-boot/exceptionTest/src/main/java/com/fanxb/exceptiontest/ExceptionTestApplication.java new file mode 100644 index 0000000..371c6b2 --- /dev/null +++ b/spring-boot/exceptionTest/src/main/java/com/fanxb/exceptiontest/ExceptionTestApplication.java @@ -0,0 +1,13 @@ +package com.fanxb.exceptiontest; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class ExceptionTestApplication { + + public static void main(String[] args) { + SpringApplication.run(ExceptionTestApplication.class, args); + } + +} diff --git a/spring-boot/exceptionTest/src/main/java/com/fanxb/exceptiontest/config/ExceptionHandle.java b/spring-boot/exceptionTest/src/main/java/com/fanxb/exceptiontest/config/ExceptionHandle.java new file mode 100644 index 0000000..c493802 --- /dev/null +++ b/spring-boot/exceptionTest/src/main/java/com/fanxb/exceptiontest/config/ExceptionHandle.java @@ -0,0 +1,36 @@ +package com.fanxb.exceptiontest.config; + +import com.fanxb.exceptiontest.entity.Result; +import com.fanxb.exceptiontest.entity.exception.BaseException; +import com.fanxb.exceptiontest.entity.exception.CustomBusinessException; +import com.fasterxml.jackson.databind.ser.Serializers; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +/** + * @author fanxb + * @date 2021-09-24-下午4:37 + */ +@RestControllerAdvice +@Slf4j +public class ExceptionHandle { + @ExceptionHandler(Exception.class) + public Result handleException(Exception e) { + BaseException be; + if (e instanceof BaseException) { + be = (BaseException) e; + //手动抛出的异常,仅记录message + log.info("baseException:{}", be.getMessage()); + if (be instanceof CustomBusinessException) { + //可在这里进行一些针对特定异常的处理逻辑 + log.info("customBusinessException:{}", be.getMessage()); + } + } else { + //其它异常,非自动抛出的,无需给前端返回具体错误内容(用户不需要看见空指针之类的异常信息) + log.error("other exception:{}", e.getMessage(), e); + be = new BaseException("系统异常,请稍后重试", e); + } + return new Result(be.getCode(), be.getMessage(), null); + } +} diff --git a/spring-boot/exceptionTest/src/main/java/com/fanxb/exceptiontest/controller/TestController.java b/spring-boot/exceptionTest/src/main/java/com/fanxb/exceptiontest/controller/TestController.java new file mode 100644 index 0000000..2d60b26 --- /dev/null +++ b/spring-boot/exceptionTest/src/main/java/com/fanxb/exceptiontest/controller/TestController.java @@ -0,0 +1,30 @@ +package com.fanxb.exceptiontest.controller; + +import com.fanxb.exceptiontest.entity.Result; +import com.fanxb.exceptiontest.entity.exception.BaseException; +import com.fanxb.exceptiontest.entity.exception.CustomBusinessException; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * @author fanxb + * @date 2021-09-24-下午4:46 + */ +@RestController +public class TestController { + + @GetMapping("/test1") + public Result test1() { + throw new BaseException("我是test1", null); + } + + @GetMapping("/test2") + public Result test2() { + throw new CustomBusinessException("我是test2", null); + } + + @GetMapping("/test3") + public Result test3() { + throw new RuntimeException("我是test2"); + } +} diff --git a/spring-boot/exceptionTest/src/main/java/com/fanxb/exceptiontest/entity/Result.java b/spring-boot/exceptionTest/src/main/java/com/fanxb/exceptiontest/entity/Result.java new file mode 100644 index 0000000..42718d4 --- /dev/null +++ b/spring-boot/exceptionTest/src/main/java/com/fanxb/exceptiontest/entity/Result.java @@ -0,0 +1,34 @@ +package com.fanxb.exceptiontest.entity; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serializable; + +/** + * @author fanxb + * @date 2021-09-24-下午4:39 + */ +@Data +@AllArgsConstructor +@NoArgsConstructor +public class Result implements Serializable { + private static final long serialVersionUID = 451834802206432869L; + /** + * 1:成功,其他失败,具体数值在自定义的异常类中 + */ + private int code; + /** + * message提示内容,当接口异常时提示异常信息 + */ + private String message; + /** + * 携带真正需要返回的数据 + */ + private Object data; + + public static Result success(Object obj) { + return new Result(1, null, obj); + } +} diff --git a/spring-boot/exceptionTest/src/main/java/com/fanxb/exceptiontest/entity/exception/BaseException.java b/spring-boot/exceptionTest/src/main/java/com/fanxb/exceptiontest/entity/exception/BaseException.java new file mode 100644 index 0000000..3d37316 --- /dev/null +++ b/spring-boot/exceptionTest/src/main/java/com/fanxb/exceptiontest/entity/exception/BaseException.java @@ -0,0 +1,50 @@ +package com.fanxb.exceptiontest.entity.exception; + +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; + +/** + * @author fanxb + * @date 2021-09-24-下午5:13 + */ +@Getter +public class BaseException extends RuntimeException { + private static final long serialVersionUID = -3149747381632839680L; + /** + * 基本错误code为0 + */ + private static final int BASE_CODE = 0; + private final int code; + private final String message; + + public BaseException() { + this(null, null, null); + } + + public BaseException(String message) { + this(message, null, null); + } + + public BaseException(Exception e) { + this(null, null, e); + } + + public BaseException(String message, Exception e) { + this(message, null, e); + } + + protected BaseException(String message, Integer code, Exception e) { + super(e); + this.message = message == null ? "" : message; + this.code = code == null ? BASE_CODE : code; + } + + @Override + public String getMessage() { + if (this.message != null && this.message.length() > 0) { + return this.message; + } + return super.getMessage(); + } +} diff --git a/spring-boot/exceptionTest/src/main/java/com/fanxb/exceptiontest/entity/exception/CustomBusinessException.java b/spring-boot/exceptionTest/src/main/java/com/fanxb/exceptiontest/entity/exception/CustomBusinessException.java new file mode 100644 index 0000000..d4455cc --- /dev/null +++ b/spring-boot/exceptionTest/src/main/java/com/fanxb/exceptiontest/entity/exception/CustomBusinessException.java @@ -0,0 +1,22 @@ +package com.fanxb.exceptiontest.entity.exception; + +/** + * @author fanxb + * @date 2021-09-26-上午10:41 + */ +public class CustomBusinessException extends BaseException { + private static final long serialVersionUID = 1564935267302330109L; + + /** + * 自定义业务异常错误码 + */ + private static final int ERROR_CODE = -1; + + public CustomBusinessException() { + super("自定义义务异常", ERROR_CODE, null); + } + + public CustomBusinessException(String message, Exception e) { + super(message, ERROR_CODE, e); + } +} diff --git a/spring-boot/exceptionTest/src/main/resources/application.properties b/spring-boot/exceptionTest/src/main/resources/application.properties new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/spring-boot/exceptionTest/src/main/resources/application.properties @@ -0,0 +1 @@ +