You can configure a Validator bean by yourself. By default, it uses HibernateValidator.class, there are other options like ApacheValidationProvider.class.
A benefit of configuring it is to make it fail fast.
In failFast mode, it throws an exception on the first violation.
@Bean
public Validator validator() {
ValidatorFactory validatorFactory = Validation.byProvider( HibernateValidator.class)
.configure()
.failFast(true)
.buildValidatorFactory();
return validatorFactory.getValidator();
}
ValidatorFactory validatorFactory = Validation.byProvider( HibernateValidator.class )
.configure()
.addProperty( "hibernate.validator.fail_fast", "true" )
.buildValidatorFactory();
return validatorFactory.getValidator();
@Service
@Validated
class ValidatingService{
void validateInput(@Valid DTO dto){
// do something
}
}
https://mossgreen.github.io/Validations-in-Spring/