question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JSR validator bean missing if a custom Spring Validator bean is present

See original GitHub issue

This problem occurred after upgrade from Spring Boot 1.5.2 to 1.5.3.

I have some custom Spring validator beans for Spring Data REST while also relying on standard JSR validator in other parts of the application.

After upgrading from Spring Boot 1.5.2 to 1.5.3 there is no longer a JSR validator bean present.

Error:

Bean method 'defaultValidator' in 'DefaultValidatorConfiguration' not loaded because @ConditionalOnMissingBean (types: javax.validation.Validator,org.springframework.validation.Validator; SearchStrategy: all) found beans 'beforeSaveUserValidator', 'beforeCreateGroupValidator', 'beforeCreateUserValidator'

Debug auto configuration:

DefaultValidatorConfiguration#defaultValidator:
      Did not match:
         - @ConditionalOnMissingBean (types: javax.validation.Validator,org.springframework.validation.Validator; SearchStrategy: all) found beans 'beforeSaveUserValidator', 'beforeCreateGroupValidator', 'beforeCreateUserValidator' (OnBeanCondition)

https://github.com/spring-projects/spring-boot/commit/c9561f031c591ca55e028ca66250511c5e62002a changed this behaviour since it does not provide a validator if a Spring validator is present.

Not sure if this is should be considered a bug and I can easily work around this problem and provide this bean myself, but perhaps Spring Boot could create a JSR validator if not present?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
oyvindhornelandcommented, Apr 28, 2017

Superb! Thank you!

0reactions
asaarnakcommented, Aug 8, 2017

@snicoll Finally i had time to take a look at this issue and managed to workaround it. We were returning javax.validation.ValidationFactory as bean type so the following condition created new bean defaultValidator.

@ConditionalOnMissingBean(type = { "javax.validation.Validator", "org.springframework.validation.Validator" })

The fix was to return the LocalValidatorFactoryBean type instead of ValidatorFactory. @Bean public LocalValidatorFactoryBean beanValidatorFactory()

Read more comments on GitHub >

github_iconTop Results From Across the Web

JSR-303 validation is ignored when custom ... - Stack Overflow
I've noticed that JSR-303 validation is completely ignored in Spring when a custom Validator bean annotated with @Component is declared.
Read more >
Spring Validation Example - Spring MVC Form Validator
For using custom validator, first we need to inject it in the controller class. We are using spring bean auto wiring to achieve...
Read more >
Validation in Spring Boot - Baeldung
Learn how to validate domain objects in Spring Boot using Hibernate Validator, the reference implementation of the Bean Validation ...
Read more >
Validation with Hibernate Validator - Quarkus
This guide covers how to use Hibernate Validator/Bean Validation for: validating the input/output of your REST services;. validating the parameters and ...
Read more >
Request Validation in Spring Boot - SpringExamples
Bean Validation 2.0 (JSR 380) is the specification of the Java API for JavaBean validation that provides a class-level constraint ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found