Feb 11th, 2013, 08:34 PM
JSR303 bean method validation on non getter method?
I would like to use JSR 303 to validate beans in my application context. I have a small @Configuration that adds three beans:
MySpecialBean has one method: public @NotNull String getName() whose implementation always returns null. When I load the application context I get a validation error (as expected). However if I rename the method from getName() to name() the context starts normally (no error). As if the validation does not occur.
I am using Spring 3.2.1 and Hibernate Validator 4.2.0. The documentation for HV suggests constraints can be added to any method (not just property getters). I am not sure why this isn't working.
Does anyone know what is going on? Is there a configuration option I need to change?
Feb 12th, 2013, 08:12 PM
Turns out the solution is simple:
- Add MethodValidationPostProcessor to the spring context.
- Annotate type with @Validated
Hope this helps someone as this doesn't appear to be documented.
Tags for this Thread