Announcement Announcement Module
No announcement yet.
JSR303 bean method validation on non getter method? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • ryanc
    started a topic JSR303 bean method validation on non getter method?

    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:

    - LocalValidatorFactoryBean
    - BeanValidationPostProcessor
    - MySpecialBean

    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?

  • ryanc
    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.

    Leave a comment: