title: Auto validation using hibernate validator XML configuration and Spring
body: <p>I have created RESTful service (POST) using Spring-MVC and I can access it successfully using POSTMAN.
  Now, I am trying to put validation to input json and I want to use Hibernate validator using its XML configuration. But validation is not triggering.</p>
  
  <p>This is my service signature looks like:</p>
  
  <pre><code>@RequestMapping(value = "/checkmycar", method = RequestMethod.POST, consumes = "application/json")
  public String check(@Valid @RequestBody Car car, BindingResult result) {
  
      if (result.hasErrors()) {
          return "ERROR";
      }
  
      else
          return car.getManufacturer();
  }
  </code></pre>
  
  <p>Following is my POM.xml</p>
  
  <pre><code>&lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"&gt;
  &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  &lt;groupId&gt;test&lt;/groupId&gt;
  &lt;artifactId&gt;test.hibernate-validator&lt;/artifactId&gt;
  &lt;packaging&gt;war&lt;/packaging&gt;
  &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
  &lt;dependencies&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt;
          &lt;version&gt;3.2.0.RELEASE&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.codehaus.jackson&lt;/groupId&gt;
          &lt;artifactId&gt;jackson-mapper-asl&lt;/artifactId&gt;
          &lt;version&gt;1.9.10&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
          &lt;artifactId&gt;hibernate-validator&lt;/artifactId&gt;
          &lt;version&gt;4.2.0.Final&lt;/version&gt;
      &lt;/dependency&gt;
  
  &lt;/dependencies&gt;
  </code></pre>
  
  <p></p>
  
  <p>I am referring to <a href="http://docs.jboss.org/hibernate/validator/4.2/reference/en-US/html_single/" rel="nofollow">this</a> document. And have created validation xml</p>
  
  <pre><code>&lt;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"&gt;
  &lt;default-package&gt;org.hibernate.validator.quickstart&lt;/default-package&gt;
  &lt;bean class="Car" ignore-annotations="true"&gt;
      &lt;field name="manufacturer"&gt;
          &lt;constraint annotation="javax.validation.constraints.NotNull"/&gt;
      &lt;/field&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p></p>
  
  <p>constraints-test.xml</p>
  
  <pre><code>&lt;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"&gt;
  &lt;default-package&gt;org.hibernate.validator.quickstart&lt;/default-package&gt;
  &lt;bean class="Car" ignore-annotations="true"&gt;
      &lt;field name="manufacturer"&gt;
          &lt;constraint annotation="javax.validation.constraints.NotNull"/&gt;
      &lt;/field&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p></p>
  
  <p>I have placed both constraints-test.xml and validation.xml in src/main/resources.</p>
  
  <p>Car.java</p>
  
  <pre><code>public class Car {
  
  private String manufacturer;
  
  public Car() {
  }
  
  public Car(String manufacturer) {
      this.manufacturer = manufacturer;
  }
  public String getManufacturer() {
      return manufacturer;
  }
  
  public void setManufacturer(String manufacturer) {
      this.manufacturer = manufacturer;
  }
  }
  </code></pre>
  
  <p>Now when I trigger the service with following json</p>
  
  <pre><code>{
  "manufacturer":"TOYOTA"
  }
  </code></pre>
  
  <p>I get {"TOYOTA"}</p>
  
  <p>when I send invalid json</p>
  
  <p>{
      "manufacturer":null
      }</p>
  
  <p>I get {null}</p>
  
  <p>Can someone please tell me why the hibernate validation is not getting triggered?</p>
  
  <p>Thanks in advance</p>