Search in sources :

Example 11 with Validator

use of org.springframework.validation.Validator in project spring-framework by spring-projects.

the class WebFluxConfigurationSupport method webFluxValidator.

/**
	 * Return a global {@link Validator} instance for example for validating
	 * {@code @RequestBody} method arguments.
	 * <p>Delegates to {@link #getValidator()} first. If that returns {@code null}
	 * checks the classpath for the presence of a JSR-303 implementations
	 * before creating a {@code OptionalValidatorFactoryBean}. If a JSR-303
	 * implementation is not available, a "no-op" {@link Validator} is returned.
	 */
@Bean
public Validator webFluxValidator() {
    Validator validator = getValidator();
    if (validator == null) {
        if (ClassUtils.isPresent("javax.validation.Validator", getClass().getClassLoader())) {
            Class<?> clazz;
            try {
                String name = "org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean";
                clazz = ClassUtils.forName(name, getClass().getClassLoader());
            } catch (ClassNotFoundException ex) {
                throw new BeanInitializationException("Could not find default validator class", ex);
            } catch (LinkageError ex) {
                throw new BeanInitializationException("Could not load default validator class", ex);
            }
            validator = (Validator) BeanUtils.instantiateClass(clazz);
        } else {
            validator = new NoOpValidator();
        }
    }
    return validator;
}
Also used : BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) Validator(org.springframework.validation.Validator) Bean(org.springframework.context.annotation.Bean)

Example 12 with Validator

use of org.springframework.validation.Validator in project spring-framework by spring-projects.

the class WebMvcConfigurationSupport method mvcValidator.

/**
	 * Return a global {@link Validator} instance for example for validating
	 * {@code @ModelAttribute} and {@code @RequestBody} method arguments.
	 * Delegates to {@link #getValidator()} first and if that returns {@code null}
	 * checks the classpath for the presence of a JSR-303 implementations
	 * before creating a {@code OptionalValidatorFactoryBean}.If a JSR-303
	 * implementation is not available, a no-op {@link Validator} is returned.
	 */
@Bean
public Validator mvcValidator() {
    Validator validator = getValidator();
    if (validator == null) {
        if (ClassUtils.isPresent("javax.validation.Validator", getClass().getClassLoader())) {
            Class<?> clazz;
            try {
                String className = "org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean";
                clazz = ClassUtils.forName(className, WebMvcConfigurationSupport.class.getClassLoader());
            } catch (ClassNotFoundException | LinkageError ex) {
                throw new BeanInitializationException("Could not find default validator class", ex);
            }
            validator = (Validator) BeanUtils.instantiateClass(clazz);
        } else {
            validator = new NoOpValidator();
        }
    }
    return validator;
}
Also used : BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) Validator(org.springframework.validation.Validator) Bean(org.springframework.context.annotation.Bean)

Example 13 with Validator

use of org.springframework.validation.Validator in project spring-boot by spring-projects.

the class ConfigurationPropertiesBindingPostProcessor method determineValidator.

private Validator determineValidator(Object bean) {
    Validator validator = getValidator();
    boolean supportsBean = (validator != null && validator.supports(bean.getClass()));
    if (ClassUtils.isAssignable(Validator.class, bean.getClass())) {
        if (supportsBean) {
            return new ChainingValidator(validator, (Validator) bean);
        }
        return (Validator) bean;
    }
    return (supportsBean ? validator : null);
}
Also used : Validator(org.springframework.validation.Validator)

Example 14 with Validator

use of org.springframework.validation.Validator in project spring-boot by spring-projects.

the class ConfigurationPropertiesBindingPostProcessor method freeLocalValidator.

private void freeLocalValidator() {
    try {
        Validator validator = this.localValidator;
        this.localValidator = null;
        if (validator != null) {
            ((DisposableBean) validator).destroy();
        }
    } catch (Exception ex) {
        throw new IllegalStateException(ex);
    }
}
Also used : DisposableBean(org.springframework.beans.factory.DisposableBean) Validator(org.springframework.validation.Validator) BeanCreationException(org.springframework.beans.factory.BeanCreationException) BeansException(org.springframework.beans.BeansException) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException)

Example 15 with Validator

use of org.springframework.validation.Validator in project spring-boot by spring-projects.

the class WebFluxAnnotationAutoConfigurationTests method validationJsr303CustomValidatorReusedAsSpringValidator.

@Test
public void validationJsr303CustomValidatorReusedAsSpringValidator() {
    load(CustomValidator.class);
    assertThat(this.context.getBeansOfType(ValidatorFactory.class)).hasSize(1);
    assertThat(this.context.getBeansOfType(javax.validation.Validator.class)).hasSize(1);
    assertThat(this.context.getBeansOfType(Validator.class)).hasSize(2);
    Validator validator = this.context.getBean("webFluxValidator", Validator.class);
    assertThat(validator).isInstanceOf(SpringValidator.class);
    assertThat(((SpringValidator) validator).getTarget()).isSameAs(this.context.getBean(javax.validation.Validator.class));
}
Also used : SpringValidator(org.springframework.boot.autoconfigure.validation.SpringValidator) SpringValidator(org.springframework.boot.autoconfigure.validation.SpringValidator) Validator(org.springframework.validation.Validator) Test(org.junit.Test)

Aggregations

Validator (org.springframework.validation.Validator)19 Test (org.junit.Test)15 SpringValidator (org.springframework.boot.autoconfigure.validation.SpringValidator)8 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)3 ApplicationContext (org.springframework.context.ApplicationContext)3 Bean (org.springframework.context.annotation.Bean)3 WebExchangeDataBinder (org.springframework.web.bind.support.WebExchangeDataBinder)3 BeanInitializationException (org.springframework.beans.factory.BeanInitializationException)2 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)2 ConversionService (org.springframework.core.convert.ConversionService)2 SpringValidatorAdapter (org.springframework.validation.beanvalidation.SpringValidatorAdapter)2 ConfigurableWebBindingInitializer (org.springframework.web.bind.support.ConfigurableWebBindingInitializer)2 WebBindingInitializer (org.springframework.web.bind.support.WebBindingInitializer)2 BindingContext (org.springframework.web.reactive.BindingContext)2 FAIL_ON_UNKNOWN_PROPERTIES (com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)1 DEFAULT_VIEW_INCLUSION (com.fasterxml.jackson.databind.MapperFeature.DEFAULT_VIEW_INCLUSION)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 XmlMapper (com.fasterxml.jackson.dataformat.xml.XmlMapper)1 List (java.util.List)1 Locale (java.util.Locale)1