diff --git a/youlai-common/common-web/src/main/java/com/youlai/common/web/config/WebMvcConfig.java b/youlai-common/common-web/src/main/java/com/youlai/common/web/config/WebMvcConfig.java index e998f0cdd..93e4c3d4f 100644 --- a/youlai-common/common-web/src/main/java/com/youlai/common/web/config/WebMvcConfig.java +++ b/youlai-common/common-web/src/main/java/com/youlai/common/web/config/WebMvcConfig.java @@ -6,11 +6,12 @@ import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; import lombok.extern.slf4j.Slf4j; import org.hibernate.validator.HibernateValidator; +import org.springframework.beans.factory.config.AutowireCapableBeanFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; -import org.springframework.validation.beanvalidation.SpringValidatorAdapter; +import org.springframework.validation.beanvalidation.SpringConstraintValidatorFactory; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import javax.validation.Validation; @@ -43,17 +44,13 @@ public class WebMvcConfig implements WebMvcConfigurer { } @Bean - public Validator validator() { - ValidatorFactory validatorFactory = Validation.byProvider(HibernateValidator.class).configure() + public Validator validator(final AutowireCapableBeanFactory autowireCapableBeanFactory) { + ValidatorFactory validatorFactory = Validation.byProvider(HibernateValidator.class) + .configure() .failFast(true) // failFast=true 不校验所有参数,只要出现校验失败情况直接返回,不再进行后续参数校验 + .constraintValidatorFactory(new SpringConstraintValidatorFactory(autowireCapableBeanFactory)) .buildValidatorFactory(); return validatorFactory.getValidator(); } - - @Override - public org.springframework.validation.Validator getValidator() { - return new SpringValidatorAdapter(validator()); - } - }