spring-data-rest: Validator auto discovery not working for Spring Data Rest [DATAREST-524]
Daniel Moses opened DATAREST-524 and commented
See documentation http://docs.spring.io/spring-data/rest/docs/2.2.2.RELEASE/reference/html/#validation-chapter
Discovery should happen with Validator Prefix. Add a Validator bean to the context and notice that it does not get auto-detected. Manual wiring still works. Here is an example validator that will not work if included in the example Spring boot project:
@Component("beforeCreatePersonValidator")
public class BeforeCreatePersonValidator implements Validator {
@Override
public boolean supports(Class<?> clazz) {
return Person.class.equals(clazz);
}
@Override
public void validate(Object target, Errors errors) {
errors.reject("TESTING");
}
}
See problem as reported: http://stackoverflow.com/questions/24318405/spring-data-rest-validator
34 votes, 37 watchers
About this issue
- Original URL
- State: open
- Created 12 years ago
- Reactions: 2
- Comments: 15
In 2024, https://docs.spring.io/spring-data/rest/reference/validation.html mentioned auto wired still not work.
The above-mentioned code solved the issue on my side. I slightly modified it to also register JSR 380 bean validators:
Rafael Renan Pacheco commented
The workaround is blogged here: https://www.baeldung.com/spring-data-rest-validators
You set the events you want to register in the events array, like “beforeCreate”, and the code will look for all validators the starts with this string and register it. This way you can create your validators as components, like this:
And the workaround to load all validators that begins with “beforeCreate” is this:
If you add all possible events in the “events” array, you will get what Spring Data Rest should have been doing in the first place
Sebastian Bathke commented
Thanks Andreas for providing the configuration! Had the same problem that validation beans with this prefix didn’t got catched up as the documentation suggests.
However I encountered a problem that in some occasions (multiple repositories for the same entity) some tests failed to startup the context correctly with
I could fix that by migrating your solution to RepositoryRestConfigurerAdapter:
Andreas Kluth commented
You could add this configuration to add the expected behavior.