I tried implementing validation for my web application like described in section 5.7.4.3 of the Spring 3.0 documentation:
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="webBindingInitializer">
<bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
<property name="validator">
<bean class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
</property>
</bean>
</property>
</bean>
I've added hibernate-validator as a dependency and everything looks OK, but starting my web app results in the following error:
org.springframework.beans.NotWritablePropertyException:
Invalid property 'validator' of bean class
[org.springframework.web.bind.support.ConfigurableWebBindingInitializer]:
Bean property 'validator' is not writable or has an invalid setter method.
Does the parameter type of the setter match the return type of the getter?
When looking at the API it's pretty obvious that something's wrong. LocalValidatorFactoryBean
implements javax.validation.Validator
while ConfigurableWebBindingInitializer.setValidator()
expects a org.springframework.validation.Validator
.
Any solution for this?
EDIT
About LocalValidatorFactoryBean
:
This is the central class for
javax.validation
(JSR-303) setup in a Spring application context: It bootstraps ajavax.validation.ValidationFactory
and exposes it through the SpringValidator
interface as well as through the JSR-303Validator
interface and theValidatorFactory
interface itself.
Like Alex Marshall stated below this is not the case. LocalValidatorFactoryBean
only implements javax.validation.Validator
.
LocalValidatorFactoryBean
does extend SpringValidatorAdapter
though, which implements org.springframework.validation.Validator
.
Here's a workaround for this issue:
Using the validator
bean configuration from my question above you have to add the following code in each controller using validation.
@Autowired
private javax.validation.Validator validator;
@InitBinder
protected void initBinder(WebDataBinder binder) {
binder.setValidator((org.springframework.validation.Validator) this.validator);
}
It's dirty, but it works because LocalValidatorFactoryBean
extends SpringValidatorAdapter
and this implements org.springframework.validation.Validator
.