From fd8fe53377dabf34f08fe37ff5dfe34a945ccb58 Mon Sep 17 00:00:00 2001 From: fanxb Date: Sat, 9 Oct 2021 16:31:04 +0800 Subject: [PATCH] add --- .../exceptiontest/config/ExceptionHandle.java | 14 +++++++++ .../controller/TestController.java | 30 +++++++++++++++---- .../exception/CustomValidException.java | 18 +++++++++++ .../exceptiontest/entity/vo/TestBody5.java | 17 +++++++++++ 4 files changed, 74 insertions(+), 5 deletions(-) create mode 100644 spring-boot/paramsCheck/src/main/java/com/fanxb/exceptiontest/entity/exception/CustomValidException.java create mode 100644 spring-boot/paramsCheck/src/main/java/com/fanxb/exceptiontest/entity/vo/TestBody5.java 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 index c493802..ada173c 100644 --- 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 @@ -3,11 +3,16 @@ 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.fanxb.exceptiontest.entity.exception.CustomValidException; import com.fasterxml.jackson.databind.ser.Serializers; import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; +import javax.validation.ConstraintViolationException; +import java.util.Objects; + /** * @author fanxb * @date 2021-09-24-下午4:37 @@ -26,6 +31,15 @@ public class ExceptionHandle { //可在这里进行一些针对特定异常的处理逻辑 log.info("customBusinessException:{}", be.getMessage()); } + } else if (e instanceof ConstraintViolationException) { + //url参数、数组类参数校验报错类 + ConstraintViolationException ce = (ConstraintViolationException) e; + //针对参数校验异常,建立了一个异常类 + be = new CustomValidException(ce.getMessage()); + } else if (e instanceof MethodArgumentNotValidException) { + //json对象类参数校验报错类 + MethodArgumentNotValidException ce = (MethodArgumentNotValidException) e; + be = new CustomValidException(Objects.requireNonNull(ce.getFieldError()).getDefaultMessage()); } else { //其它异常,非自动抛出的,无需给前端返回具体错误内容(用户不需要看见空指针之类的异常信息) log.error("other exception:{}", e.getMessage(), e); 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 index 039344d..a6d8fb0 100644 --- 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 @@ -5,18 +5,19 @@ 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 com.fanxb.exceptiontest.entity.vo.TestBody4; +import com.fanxb.exceptiontest.entity.vo.*; +import lombok.extern.slf4j.Slf4j; import org.hibernate.validator.constraints.Range; import org.springframework.validation.BindException; +import org.springframework.validation.BindingResult; 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; +import java.util.List; +import java.util.Objects; /** * @author fanxb @@ -24,6 +25,7 @@ import javax.validation.constraints.NotBlank; */ @RestController @Validated +@Slf4j public class TestController { /** @@ -70,8 +72,26 @@ public class TestController { * 自定义校验 */ @PostMapping("/test6") - public Result test6(@Validated @RequestBody TestBody4 testBody) { + public Result test6(@Validated @RequestBody TestBody4 testBody,BindingResult result) { + if(result.hasErrors()){ + log.info("asdf"); + } return null; } + /** + * 集合校验1(对象内对象集合) + */ + @PostMapping("/test7") + public Result test7(@Validated @RequestBody TestBody5 testBody) { + return null; + } + + /** + * 集合校验2(对象集合) + */ + @PostMapping("/test8") + public Result test8(@Validated @RequestBody List<@Valid TestBody4> list) { + return null; + } } diff --git a/spring-boot/paramsCheck/src/main/java/com/fanxb/exceptiontest/entity/exception/CustomValidException.java b/spring-boot/paramsCheck/src/main/java/com/fanxb/exceptiontest/entity/exception/CustomValidException.java new file mode 100644 index 0000000..011d8f0 --- /dev/null +++ b/spring-boot/paramsCheck/src/main/java/com/fanxb/exceptiontest/entity/exception/CustomValidException.java @@ -0,0 +1,18 @@ +package com.fanxb.exceptiontest.entity.exception; + +/** + * @author fanxb + * @date 2021-10-09-下午4:06 + */ +public class CustomValidException extends BaseException { + private static final long serialVersionUID = -5540634625578625266L; + + /** + * 自定义业务异常错误码 + */ + private static final int ERROR_CODE = -2; + + public CustomValidException(String message) { + super(message, ERROR_CODE, null); + } +} diff --git a/spring-boot/paramsCheck/src/main/java/com/fanxb/exceptiontest/entity/vo/TestBody5.java b/spring-boot/paramsCheck/src/main/java/com/fanxb/exceptiontest/entity/vo/TestBody5.java new file mode 100644 index 0000000..8b8adbe --- /dev/null +++ b/spring-boot/paramsCheck/src/main/java/com/fanxb/exceptiontest/entity/vo/TestBody5.java @@ -0,0 +1,17 @@ +package com.fanxb.exceptiontest.entity.vo; + +import com.fanxb.exceptiontest.entity.validation.annotation.CustomCheck; +import lombok.Data; + +import javax.validation.Valid; +import java.util.List; + +/** + * @author fanxb + * @date 2021-09-28-下午4:29 + */ +@Data +public class TestBody5 { + @Valid + private List list; +}