diff --git a/spring-boot/paramsCheck/.gitignore b/spring-boot/paramsCheck/.gitignore new file mode 100644 index 0000000..549e00a --- /dev/null +++ b/spring-boot/paramsCheck/.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/paramsCheck/pom.xml b/spring-boot/paramsCheck/pom.xml new file mode 100644 index 0000000..cab5d10 --- /dev/null +++ b/spring-boot/paramsCheck/pom.xml @@ -0,0 +1,59 @@ + + + 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-starter-validation + 2.5.5 + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + ${project.parent.version} + + + + org.projectlombok + lombok + + + + + + + + diff --git a/spring-boot/paramsCheck/src/main/java/com/fanxb/exceptiontest/ExceptionTestApplication.java b/spring-boot/paramsCheck/src/main/java/com/fanxb/exceptiontest/ExceptionTestApplication.java new file mode 100644 index 0000000..371c6b2 --- /dev/null +++ b/spring-boot/paramsCheck/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/paramsCheck/src/main/java/com/fanxb/exceptiontest/config/ExceptionHandle.java b/spring-boot/paramsCheck/src/main/java/com/fanxb/exceptiontest/config/ExceptionHandle.java new file mode 100644 index 0000000..c493802 --- /dev/null +++ b/spring-boot/paramsCheck/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/paramsCheck/src/main/java/com/fanxb/exceptiontest/controller/TestController.java b/spring-boot/paramsCheck/src/main/java/com/fanxb/exceptiontest/controller/TestController.java new file mode 100644 index 0000000..31b30ea --- /dev/null +++ b/spring-boot/paramsCheck/src/main/java/com/fanxb/exceptiontest/controller/TestController.java @@ -0,0 +1,68 @@ +package com.fanxb.exceptiontest.controller; + +import com.fanxb.exceptiontest.entity.Result; +import com.fanxb.exceptiontest.entity.consistant.Insert; +import com.fanxb.exceptiontest.entity.consistant.Update; +import com.fanxb.exceptiontest.entity.exception.BaseException; +import com.fanxb.exceptiontest.entity.exception.CustomBusinessException; +import com.fanxb.exceptiontest.entity.vo.TestBody; +import com.fanxb.exceptiontest.entity.vo.TestBody2; +import com.fanxb.exceptiontest.entity.vo.TestBody3; +import org.hibernate.validator.constraints.Range; +import org.springframework.validation.BindException; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; + +import javax.validation.Valid; +import javax.validation.constraints.Email; +import javax.validation.constraints.NotBlank; + +/** + * @author fanxb + * @date 2021-09-24-下午4:46 + */ +@RestController +@Validated +public class TestController { + + /** + * url参数校验 + */ + @GetMapping("/test1") + public Result test1(@NotBlank @RequestParam String param1, @Range(max = 100, min = 10) @RequestParam int param2) { + return null; + } + + /** + * 表单校验 + */ + @PostMapping("/test2") + public Result test2(@Valid @RequestBody TestBody body) { + return null; + } + + /** + * 嵌套校验 + */ + @PostMapping("/test3") + public Result test3(@Valid @RequestBody TestBody2 testBody2) { + throw new RuntimeException("我是test2"); + } + + /** + * 分组校验1 + */ + @PostMapping("/test4") + public Result test4(@Validated({Update.class}) @RequestBody TestBody3 testBody) { + return null; + } + + /** + * 分组校验2 + */ + @PostMapping("/test5") + public Result test5(@Validated({Insert.class}) @RequestBody TestBody3 testBody) { + return null; + } + +} diff --git a/spring-boot/paramsCheck/src/main/java/com/fanxb/exceptiontest/entity/Result.java b/spring-boot/paramsCheck/src/main/java/com/fanxb/exceptiontest/entity/Result.java new file mode 100644 index 0000000..3fd871a --- /dev/null +++ b/spring-boot/paramsCheck/src/main/java/com/fanxb/exceptiontest/entity/Result.java @@ -0,0 +1,41 @@ +package com.fanxb.exceptiontest.entity; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import org.hibernate.validator.constraints.Range; +import org.springframework.validation.annotation.Validated; + +import javax.validation.constraints.Email; +import javax.validation.constraints.NotNull; +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:成功,其他失败,具体数值在自定义的异常类中 + */ + @NotNull + @Email + @Range + 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/paramsCheck/src/main/java/com/fanxb/exceptiontest/entity/consistant/Insert.java b/spring-boot/paramsCheck/src/main/java/com/fanxb/exceptiontest/entity/consistant/Insert.java new file mode 100644 index 0000000..ad65f71 --- /dev/null +++ b/spring-boot/paramsCheck/src/main/java/com/fanxb/exceptiontest/entity/consistant/Insert.java @@ -0,0 +1,8 @@ +package com.fanxb.exceptiontest.entity.consistant; + +/** + * @author fanxb + * @date 2021-09-28-下午5:05 + */ +public interface Insert { +} diff --git a/spring-boot/paramsCheck/src/main/java/com/fanxb/exceptiontest/entity/consistant/Update.java b/spring-boot/paramsCheck/src/main/java/com/fanxb/exceptiontest/entity/consistant/Update.java new file mode 100644 index 0000000..7dbbc44 --- /dev/null +++ b/spring-boot/paramsCheck/src/main/java/com/fanxb/exceptiontest/entity/consistant/Update.java @@ -0,0 +1,8 @@ +package com.fanxb.exceptiontest.entity.consistant; + +/** + * @author fanxb + * @date 2021-09-28-下午5:05 + */ +public interface Update { +} diff --git a/spring-boot/paramsCheck/src/main/java/com/fanxb/exceptiontest/entity/exception/BaseException.java b/spring-boot/paramsCheck/src/main/java/com/fanxb/exceptiontest/entity/exception/BaseException.java new file mode 100644 index 0000000..3d37316 --- /dev/null +++ b/spring-boot/paramsCheck/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/paramsCheck/src/main/java/com/fanxb/exceptiontest/entity/exception/CustomBusinessException.java b/spring-boot/paramsCheck/src/main/java/com/fanxb/exceptiontest/entity/exception/CustomBusinessException.java new file mode 100644 index 0000000..d4455cc --- /dev/null +++ b/spring-boot/paramsCheck/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/paramsCheck/src/main/java/com/fanxb/exceptiontest/entity/vo/TestBody.java b/spring-boot/paramsCheck/src/main/java/com/fanxb/exceptiontest/entity/vo/TestBody.java new file mode 100644 index 0000000..626a3da --- /dev/null +++ b/spring-boot/paramsCheck/src/main/java/com/fanxb/exceptiontest/entity/vo/TestBody.java @@ -0,0 +1,21 @@ +package com.fanxb.exceptiontest.entity.vo; + +import com.fanxb.exceptiontest.entity.consistant.Insert; +import lombok.Data; + +import javax.validation.constraints.Email; +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; + +/** + * @author fanxb + * @date 2021-09-28-下午4:29 + */ +@Data +public class TestBody { + @NotBlank + @Email(message = "请输入一个邮箱") + private String param1; + @NotNull + private String param2; +} diff --git a/spring-boot/paramsCheck/src/main/java/com/fanxb/exceptiontest/entity/vo/TestBody2.java b/spring-boot/paramsCheck/src/main/java/com/fanxb/exceptiontest/entity/vo/TestBody2.java new file mode 100644 index 0000000..4cf20b4 --- /dev/null +++ b/spring-boot/paramsCheck/src/main/java/com/fanxb/exceptiontest/entity/vo/TestBody2.java @@ -0,0 +1,24 @@ +package com.fanxb.exceptiontest.entity.vo; + +import lombok.Data; + +import javax.validation.Valid; +import javax.validation.constraints.Email; +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; + +/** + * @author fanxb + * @date 2021-09-28-下午4:29 + */ +@Data +public class TestBody2 { + @NotBlank + @Email(message = "请输入一个邮箱") + private String param1; + @NotNull + private String param2; + @NotNull + @Valid + private TestBody testBody; +} diff --git a/spring-boot/paramsCheck/src/main/java/com/fanxb/exceptiontest/entity/vo/TestBody3.java b/spring-boot/paramsCheck/src/main/java/com/fanxb/exceptiontest/entity/vo/TestBody3.java new file mode 100644 index 0000000..8ec4066 --- /dev/null +++ b/spring-boot/paramsCheck/src/main/java/com/fanxb/exceptiontest/entity/vo/TestBody3.java @@ -0,0 +1,22 @@ +package com.fanxb.exceptiontest.entity.vo; + +import com.fanxb.exceptiontest.entity.consistant.Insert; +import com.fanxb.exceptiontest.entity.consistant.Update; +import lombok.Data; + +import javax.validation.Valid; +import javax.validation.constraints.Email; +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; + +/** + * @author fanxb + * @date 2021-09-28-下午4:29 + */ +@Data +public class TestBody3 { + @NotBlank(groups = {Insert.class}) + private String param1; + @NotBlank(groups = {Update.class}) + private String param2; +} diff --git a/spring-boot/paramsCheck/src/main/resources/application.properties b/spring-boot/paramsCheck/src/main/resources/application.properties new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/spring-boot/paramsCheck/src/main/resources/application.properties @@ -0,0 +1 @@ +