fix(web): 配置 Validator 失败立即返回模式

This commit is contained in:
2024-01-22 23:02:01 +08:00
parent 39cfc5aa25
commit 1223f6052d
2 changed files with 28 additions and 5 deletions

View File

@@ -17,11 +17,18 @@
package top.charles7c.continew.starter.web.autoconfigure.exception; package top.charles7c.continew.starter.web.autoconfigure.exception;
import jakarta.annotation.PostConstruct; import jakarta.annotation.PostConstruct;
import jakarta.validation.Validation;
import jakarta.validation.Validator;
import jakarta.validation.ValidatorFactory;
import lombok.extern.slf4j.Slf4j; 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.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController; 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.Configuration;
import org.springframework.context.annotation.Import; 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.GlobalErrorHandler;
import top.charles7c.continew.starter.web.core.exception.GlobalExceptionHandler; 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) @ConditionalOnMissingBean(BasicErrorController.class)
public class GlobalExceptionHandlerAutoConfiguration { 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 @PostConstruct
public void postConstruct() { public void postConstruct() {
log.debug("[ContiNew Starter] - Auto Configuration 'Extension-Global Exception Handler' completed " + "initialization."); log.debug("[ContiNew Starter] - Auto Configuration 'Extension-Global Exception Handler' completed " + "initialization.");

View File

@@ -35,11 +35,8 @@ import org.springframework.web.multipart.MaxUploadSizeExceededException;
import top.charles7c.continew.starter.core.constant.StringConstants; import top.charles7c.continew.starter.core.constant.StringConstants;
import top.charles7c.continew.starter.core.exception.BadRequestException; import top.charles7c.continew.starter.core.exception.BadRequestException;
import top.charles7c.continew.starter.core.exception.BusinessException; 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 top.charles7c.continew.starter.web.model.R;
import java.util.Objects;
/** /**
* 全局异常处理器 * 全局异常处理器
* *
@@ -87,8 +84,8 @@ public class GlobalExceptionHandler {
@ExceptionHandler(MethodArgumentNotValidException.class) @ExceptionHandler(MethodArgumentNotValidException.class)
public R handleMethodArgumentNotValidException(MethodArgumentNotValidException e, HttpServletRequest request) { public R handleMethodArgumentNotValidException(MethodArgumentNotValidException e, HttpServletRequest request) {
log.warn("请求地址 [{}],参数验证失败。", request.getRequestURI(), e); log.warn("请求地址 [{}],参数验证失败。", request.getRequestURI(), e);
String errorMsg = ExceptionUtils.exToNull(() -> Objects.requireNonNull(e.getBindingResult().getFieldError()) String errorMsg = CollUtil.join(e
.getDefaultMessage()); .getAllErrors(), StringConstants.CHINESE_COMMA, DefaultMessageSourceResolvable::getDefaultMessage);
return R.fail(HttpStatus.BAD_REQUEST.value(), errorMsg); return R.fail(HttpStatus.BAD_REQUEST.value(), errorMsg);
} }