mirror of
				https://github.com/continew-org/continew-starter.git
				synced 2025-10-31 22:57:19 +08:00 
			
		
		
		
	fix(web): 配置 Validator 失败立即返回模式
This commit is contained in:
		| @@ -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 失败立即返回模式配置 | ||||
|      * | ||||
|      * <p> | ||||
|      * 默认情况下会校验完所有字段,然后才抛出异常。 | ||||
|      * </p> | ||||
|      */ | ||||
|     @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."); | ||||
|   | ||||
| @@ -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); | ||||
|     } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user