Dependency Injection in JSR-303 ConstraintValidator

I have the same problem as the guy from this thread. More precisely, I get the following error when trying to insert a bean into my custom validator that implements the CustomValidator interface (this is NPE when accessing the bean that I wanted to enter):

javax.validation.ValidationException: HV000028: Unexpected exception during isValid call. at org.hibernate.validator.internal.engine.ConstraintTree.validateSingleConstraint(ConstraintTree.java:294) at org.hibernate.validator.internal.engine.ConstraintTree.validateConstraints(ConstraintTree.java:164) at org.hibernate.validator.internal.engine.ConstraintTree.validateConstraints(ConstraintTree.java:125) at org.hibernate.validator.internal.metadata.core.MetaConstraint.validateConstraint(MetaConstraint.java:86) 

...

Raised by: java.lang.NullPointerException

Do you have a solution for this? Maybe an example? Because I tried the solutions offered in another thread, and nothing worked.

Any help is appreciated. Thanks.

0
spring dependency-injection bean-validation customvalidator
source share
1 answer

You cannot create a factory validator yourself ( Validation.buildDefaultValidatorFactory() ) if you want to use Spring constraint validators.

You should let Spring auto-negotiate the factory on the bean correctly:

 @Controller public class MyController { @Autowired private ValidatorFactory validatorFactory; // ... handler methods } 
0
source share

All Articles