Using JSR-303 with custom validation

Dear Spring Community,

I am trying to implement the following:

  • I would like to have my own validator for each controller ( via@InitBinder )
  • I would like Spring to call validator.validate()(therefore not this way )
  • I would like to use the JSR-303 annotation @Validfor this
  • The bean for validation ( RegistrationForm) does not contain JSR-303 annotations for each field.
  • I do not want to include a validation implementation (such as Hibernate ) in the classpath; it will be useless as stated above.

I basically follow the steps below here :

  • I add javax.validation.validation-api:validation-apias my addiction
  • I use <mvc:annotation-driven />
  • I mark my model with @Valid: public String onRegistrationFormSubmitted(@ModelAttribute("registrationForm") @Valid RegistrationForm registrationForm, BindingResult result) ...

So what happens is that the validation API tries to find any implementation and fails:

Caused by: javax.validation.ValidationException: Unable to find a default provider
    at javax.validation.Validation$GenericBootstrapImpl.configure(Validation.java:264)
    at org.springframework.validation.beanvalidation.LocalValidatorFactoryBean.afterPropertiesSet(LocalValidatorFactoryBean.java:183)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)

The conclusion is to define a property validatorfor AnnotationDrivenBeanDefinitionParser:

<bean name="validator" class="org.company.module.RegistrationFormValidator" />

<mvc:annotation-driven validator="validator" />

but this approach means that the validator will be installed for all controllers on ConfigurableWebBindingInitializer.initBinder().

I understand that I am trying to use the framework in a special way, but what the community will say if there is a special value for the property validatorthat says that the validator does not need permission, for example

<mvc:annotation-driven validator="manual" />

with special processing:

--- AnnotationDrivenBeanDefinitionParser.java.orig      2011-06-30 14:33:10.287577300 +0200
+++ AnnotationDrivenBeanDefinitionParser.java   2011-06-30 14:34:27.897449000 +0200
@@ -152,6 +152,10 @@

        private RuntimeBeanReference getValidator(Element element, Object source, ParserContext parserContext) {
                if (element.hasAttribute("validator")) {
+                       if ("manual".equals(element.getAttribute("validator"))) {
+                               return null;
+                       }
+
                        return new RuntimeBeanReference(element.getAttribute("validator"));
                }
                else if (jsr303Present) {

Any feedback is appreciated.

PS Repost from the Spring Forum .

+3
source share
3 answers

/ . , , .

, , @Valid, Spring ( , 3.1.1.RELEASE) ( org.springframework.web.method.annotation.ModelAttributeMethodProcessor ). , javax.validation.validation-api:validation-api , javax.validation.ValidationException: Unable to find a default provider.

/**
 * Validate the model attribute if applicable.
 * <p>The default implementation checks for {@code @javax.validation.Valid}.
 * @param binder the DataBinder to be used
 * @param parameter the method parameter
 */
protected void validateIfApplicable(WebDataBinder binder, MethodParameter parameter) {
    Annotation[] annotations = parameter.getParameterAnnotations();
    for (Annotation annot : annotations) {
        if (annot.annotationType().getSimpleName().startsWith("Valid")) {
            Object hints = AnnotationUtils.getValue(annot);
            binder.validate(hints instanceof Object[] ? (Object[]) hints : new Object[] {hints});
        }
    }
}
+2

Hibernate Validator? - JSR , - ( ).

JDBC - JDBC? JPA ? ?

, Hibernate Validator JSR-303, - , Hibernate Validator, .

+1

You wrote:

I add javax.validation.validation-api: validation-api as my dependency ...

Causes: javax.validation.ValidationException: cannot find default provider

You will also need an implementation of this api. For example, Hibernate Validator, this is the default implementation. (has nothing to do with Hibernate ORM)

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>4.2.0.Final</version>
</dependency>
0
source

All Articles