diff --git a/continew-starter-web/src/main/java/top/charles7c/continew/starter/web/autoconfigure/exception/GlobalExceptionHandlerAutoConfiguration.java b/continew-starter-web/src/main/java/top/charles7c/continew/starter/web/autoconfigure/exception/GlobalExceptionHandlerAutoConfiguration.java index a47b852d..58a878af 100644 --- a/continew-starter-web/src/main/java/top/charles7c/continew/starter/web/autoconfigure/exception/GlobalExceptionHandlerAutoConfiguration.java +++ b/continew-starter-web/src/main/java/top/charles7c/continew/starter/web/autoconfigure/exception/GlobalExceptionHandlerAutoConfiguration.java @@ -17,11 +17,18 @@ package top.charles7c.continew.starter.web.autoconfigure.exception; import jakarta.annotation.PostConstruct; +import jakarta.validation.Validation; +import jakarta.validation.Validator; +import jakarta.validation.ValidatorFactory; import lombok.extern.slf4j.Slf4j; +import org.hibernate.validator.HibernateValidator; +import org.springframework.beans.factory.config.AutowireCapableBeanFactory; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; +import org.springframework.validation.beanvalidation.SpringConstraintValidatorFactory; import top.charles7c.continew.starter.web.core.exception.GlobalErrorHandler; import top.charles7c.continew.starter.web.core.exception.GlobalExceptionHandler; @@ -37,6 +44,25 @@ import top.charles7c.continew.starter.web.core.exception.GlobalExceptionHandler; @ConditionalOnMissingBean(BasicErrorController.class) public class GlobalExceptionHandlerAutoConfiguration { + /** + * Validator 失败立即返回模式配置 + * + *
+ * 默认情况下会校验完所有字段,然后才抛出异常。 + *
+ */ + @Bean + public Validator validator(AutowireCapableBeanFactory beanFactory) { + ValidatorFactory validatorFactory = Validation.byProvider(HibernateValidator.class) + .configure() + .failFast(true) + .constraintValidatorFactory(new SpringConstraintValidatorFactory(beanFactory)) + .buildValidatorFactory(); + try (validatorFactory) { + return validatorFactory.getValidator(); + } + } + @PostConstruct public void postConstruct() { log.debug("[ContiNew Starter] - Auto Configuration 'Extension-Global Exception Handler' completed " + "initialization."); diff --git a/continew-starter-web/src/main/java/top/charles7c/continew/starter/web/core/exception/GlobalExceptionHandler.java b/continew-starter-web/src/main/java/top/charles7c/continew/starter/web/core/exception/GlobalExceptionHandler.java index c68de3ba..343e19c2 100644 --- a/continew-starter-web/src/main/java/top/charles7c/continew/starter/web/core/exception/GlobalExceptionHandler.java +++ b/continew-starter-web/src/main/java/top/charles7c/continew/starter/web/core/exception/GlobalExceptionHandler.java @@ -35,11 +35,8 @@ import org.springframework.web.multipart.MaxUploadSizeExceededException; import top.charles7c.continew.starter.core.constant.StringConstants; import top.charles7c.continew.starter.core.exception.BadRequestException; import top.charles7c.continew.starter.core.exception.BusinessException; -import top.charles7c.continew.starter.core.util.ExceptionUtils; import top.charles7c.continew.starter.web.model.R; -import java.util.Objects; - /** * 全局异常处理器 * @@ -87,8 +84,8 @@ public class GlobalExceptionHandler { @ExceptionHandler(MethodArgumentNotValidException.class) public R handleMethodArgumentNotValidException(MethodArgumentNotValidException e, HttpServletRequest request) { log.warn("请求地址 [{}],参数验证失败。", request.getRequestURI(), e); - String errorMsg = ExceptionUtils.exToNull(() -> Objects.requireNonNull(e.getBindingResult().getFieldError()) - .getDefaultMessage()); + String errorMsg = CollUtil.join(e + .getAllErrors(), StringConstants.CHINESE_COMMA, DefaultMessageSourceResolvable::getDefaultMessage); return R.fail(HttpStatus.BAD_REQUEST.value(), errorMsg); }