Omar Al Kababji
Omar Al Kababji

Reputation: 1828

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.

Upvotes: 4

Views: 12585

Answers (3)

Andrew Wynham
Andrew Wynham

Reputation: 2398

I stumbled across this while looking for something else but wanted to clarify to the OP what is happening. You do in fact need the file to exist at META-INF/validation.xml; however, that is relative to the classpath which is why it worked when you put it under WEB-INF/classes/META-INF/validation.xml.

The cleaner approach is to let the file be put there for you. Your Eclipse project should already be outputting whatever is in your source directory to WEB-INF/classes somehow for you or nothing would be running. But sometimes there are filters on what it outputs so it might excluding something. You might want to check your src dirs and make sure they don't have exclusions.

Just as an example, if you had a Maven war project, all of your java sources would go in src/main/java and the generated classes would end up in the WEB-INF/classes directory. The equivalent happens for src/main/resources which contains non-source files. When I want *.xml, *.properties, etc. to end up in WEB-INF/classes I put them in src/main/resources. For your example I would have a src/main/resources/META-INF/validation.xml file.

Hope this helps anyone else who comes across this and is confused.

Upvotes: 1

Omar Al Kababji
Omar Al Kababji

Reputation: 1828

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.

Upvotes: 10

Ugo
Ugo

Reputation: 81

Try to put your validation.xml directly into the WEB-INF/ directory.

Upvotes: 1

Related Questions