Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring-Boot How to properly inject javax.validation.Validator

I've got a problem injecting the Validator into the spring application bean when attempting to validate a model using JSR-303 (hibernate-validator)

My main configuration class is:

@EnableAutoConfiguration @EnableWebMvc // <--- @EnableJpaRepositories("com.example") @EntityScan("com.example") public class MainConfiguration { 

According to the javadocs:

/**  * Provide a custom {@link Validator} instead of the one created by default.  * The default implementation, assuming JSR-303 is on the classpath, is:  * {@link org.springframework.validation.beanvalidation.LocalValidatorFactoryBean}.  * Leave the return value as {@code null} to keep the default.  */ Validator getValidator(); 

Hibernate-validator is on the classpath. I'm trying to inject it into the Repository:

@Repository public class UserRepositoryImpl implements UserRepositoryCustom    {      @Autowired     private Validator validator; 

Exception being thrown:

 No qualifying bean of type [javax.validation.Validator] found for dependency: 

UPDATE:

The partial work-around for this is to define this in the main configuration class:

  @Bean     public Validator validator() {          return new org.springframework.validation.beanvalidation.LocalValidatorFactoryBean();     } 

But integration tests (the ones which require org.springframework.test.context.web.WebAppConfiguration; annotation and use validation logic) fail.

like image 960
WeMakeSoftware Avatar asked May 12 '14 08:05

WeMakeSoftware


People also ask

Is javax validation Validator thread-safe?

The ValidatorFactory object built by the bootstrap process should be cached and shared amongst Validator consumers. This class is thread-safe.

Is @valid and @validated the same?

The @Valid annotation ensures the validation of the whole object. Importantly, it performs the validation of the whole object graph. However, this creates issues for scenarios needing only partial validation. On the other hand, we can use @Validated for group validation, including the above partial validation.


1 Answers

You need to declare a bean of type LocalValidatorFactoryBean like this:

<bean id="validator"     class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/> 

in XML or

@Bean public javax.validation.Validator localValidatorFactoryBean() {    return new LocalValidatorFactoryBean(); } 

in Java Config.

Edit:

It is important to understand that if JPA is being used and is backed by Hibernate, then Hibernate will try to automatically validate your Beans as well as the Spring Framework. This can lead to the problem of javax.validation.ValidationException: HV000064: Unable to instantiate ConstraintValidator because Hibernate doesn't know about the Spring Context and as far as I can tell there is no way to tell it, not even with the LocalValidatorFactoryBean. This causes the Validator's to run twice. One correctly, and once that fails.

In order to disable the default Hibernate ORM validation, the following property for Spring needs to be set:

spring.jpa.properties.javax.persistence.validation.mode=none

I updated this example, because it was the one I kept finding over and over again about the Validator's not being injected, and it turns out this was the problem I faced.

This part of the Spring documentation has all the details

like image 163
geoand Avatar answered Sep 22 '22 03:09

geoand