Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bean-validation validation.xml ignored

I am using JSR 303 Bean validation in my JSF 2.0 web application and it works fine with annotations. Now I would like to ignore annotations and configure validation rules using the validation.xml file, so this is what I did (I am using an eclipse dynamic web project) :

  1. Added validation.xml under WebContent/META-INF/validation.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <validation-config
    xmlns="http://jboss.org/xml/ns/javax/validation/configuration"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/configuration validation-configuration-1.0.xsd"
    >
    
      <constraint-mapping>META-INF/validation/constraint-mapping.xml</constraint-mapping>
    
    </validation-config>
    
  2. Then created the file constraint-mapping.xml under WebContent/META-INF/validation/constraint-mapping.xml

    <constraint-mappings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/mapping validation-mapping-1.0.xsd"
                 xmlns="http://jboss.org/xml/ns/javax/validation/mapping">
    
    <bean class="my.full.path.ValidationMB" ignore-annotations="true">
    
    </bean>
    
    </constraint-mappings>
    

Having these configurations in place, I suppose the annotations in my bean class ValidationMB shall be ignored, BUT this is not happening!, which makes me assume that the validation.xml file is not being loaded.

any ideas? thanks.

Environment:

  1. Apache Tomcat 7.0.23
  2. javax.faces-2.1.4.jar
  3. hibernate-validator-4.2.0.Final.jar
  4. hibernate-validator-annotation-processor-4.2.0.Final.jar
  5. validation-api-1.0.0.GA.jar
  6. slf4j-api-1.6.1.jar

From the spec: section 4.4.6. XML Configuration: META-INF/validation.xml

Unless explicitly ignored by calling Configuration.ignoreXMLConfiguration(), a Configuration takes into account the configuration available in META-INF/validation.xml. This configuration file is optional but can be used by applications to refine some of the Bean Validation behavior. If more than one META-INF/validation.xml file is found in the classpath, a ValidationException is raised.

like image 932
Omar Al Kababji Avatar asked Sep 08 '25 14:09

Omar Al Kababji


1 Answers

To solve my problem I had to create a META-INF folder under the project src folder, which ends in the WEB-INF/classes/META-INF.

The structure of the web application is:

ROOT
|_META-INF -- don't put validation.xml here
|_WEB-INF
    |__ classes
           |_META-INF
                |__validation.xml

But I think that if I pack my web application in a jar file and reuse it in another project It may not work, I will let you know later once I do it.

like image 138
Omar Al Kababji Avatar answered Sep 10 '25 04:09

Omar Al Kababji