代码语言:javascript复制
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.Validator;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* <p>Description: </p>
* <p>Copyright: Copyright (c)2,020</p>
* <p>Company: tope</p>
* <P>Created Date :2020/11/24</P>
*
* @author xiepanpan
* @version 1.0
*/
@Configuration
public class ValidatorConfiguration implements WebMvcConfigurer {
@Autowired
private MessageSource messageSource;
@Override
public Validator getValidator() {
return validator();
}
@Bean
public Validator validator() {
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
validator.setValidationMessageSource(messageSource);
return validator;
}
}
代码语言:javascript复制 <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
参考博客: https://www.jianshu.com/p/46eda1f96abe?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation