title: Spring - Validation with @Valid is never called
body: <p>Trying to simply validate a field of my bean and instead of doing it manually wanted to check out Spring Validation, but didn't have much luck as of now.</p>
  
  <p><strong>In short:</strong></p>
  
  <p>Validation with <code>@Valid</code> annotation seems to never be called when I call a method of my <code>@RestController</code></p>
  
  <p><strong>My code:</strong></p>
  
  <p><code>pom.xml</code> (for the validation part)</p>
  
  <pre><code>&lt;dependency&gt;
      &lt;groupId&gt;javax.validation&lt;/groupId&gt;
      &lt;artifactId&gt;validation-api&lt;/artifactId&gt;
      &lt;version&gt;1.1.0.Final&lt;/version&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>Spring is version 4.1.1</p>
  
  <p>Validator</p>
  
  <pre><code>package mypackage;
  
  import org.springframework.validation.Errors;
  import org.springframework.validation.Validator;
  import org.springframework.web.bind.WebDataBinder;
  import org.springframework.web.bind.annotation.InitBinder;
  
  public class UtenteValidator implements Validator{
  
      public UtenteValidator() {
          // TODO Auto-generated constructor stub
      }
  
      @Override
      public boolean supports(Class&lt;?&gt; clazz) {
          return UtenteLite.class.equals(clazz);
      }
  
  //validation test
      @Override
      public void validate(Object target, Errors errors) {
          UtenteLite user = (UtenteLite) target;
  
            if(user.getName != "foo") {
                errors.rejectValue("name", "name not correct");
            }
  
      }
  
  
  
  
  }
  </code></pre>
  
  <p>Controller</p>
  
  <pre><code>package myPackage;
  
  import javax.validation.Valid;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.http.HttpStatus;
  import org.springframework.http.ResponseEntity;
  import org.springframework.ui.Model;
  import org.springframework.validation.BindingResult;
  import org.springframework.validation.annotation.Validated;
  import org.springframework.web.bind.WebDataBinder;
  import org.springframework.web.bind.annotation.InitBinder;
  import org.springframework.web.bind.annotation.ModelAttribute;
  import org.springframework.web.bind.annotation.PathVariable;
  import org.springframework.web.bind.annotation.RequestBody;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  import org.springframework.web.bind.annotation.ResponseBody;
  import org.springframework.web.bind.annotation.RestController;
  
  
  @RestController
  @RequestMapping("/users")
  public class UsersController {
  
  
      public UsersController() {
  
      }
  
  //tried to put @InitBinder, but no luck
      @InitBinder
      protected void initBinder(WebDataBinder binder) {
          binder.setValidator(new UtenteValidator());
      }
  
      @ResponseBody
      @RequestMapping(value="", method=RequestMethod.PUT)
      public &lt;T&gt; ResponseEntity&lt;T&gt; aggiornaUtente(@RequestBody @Valid UtenteLite utente, BindingResult result)
      {
          ResponseEntity&lt;T&gt; responseEntity=null;
  
          return responseEntity;
      }
  
  
  }
  </code></pre>
  
  <p>Te <code>BindingResult</code> result object shows always zero errors and the <code>validate</code>, <code>supports</code> or <code>initBinder</code> methods are never called.</p>
  
  <p>Found this <a href="http://www.logicbig.com/tutorials/spring-framework/spring-web-mvc/spring-custom-property-editor/" rel="nofollow noreferrer">tutorial</a> that reads:</p>
  
  <blockquote>
    <p><strong>When @InitBinder methods get called?</strong> </p>
    
    <p>The @InitBinder annotated methods
    will get called on each HTTP request if we don't specify the 'value'
    element of this annotation.</p>
    
    <p>WebDataBinder argument is specific to a model attribute. That means
    each time a model attribute is created by Spring this method will get
    called with a new instance of WebDataBinder.</p>
  </blockquote>
  
  <p>So I tried to change my controller method to this adding a <code>@ModelAttribute</code> <strong>and NOW the validation code gets called BUT the requestBody object (the "utente" object) is empty</strong>, so validation always fails because the fields are all nulls:</p>
  
  <pre><code>@ResponseBody
      @RequestMapping(value="", method=RequestMethod.PUT)
      public &lt;T&gt; ResponseEntity&lt;T&gt; aggiornaUtente(@RequestBody @Valid @ModelAttribute("utente") UtenteLite utente, BindingResult result)
      { 
  ...
  }
  </code></pre>
  
  <p>The <code>utente</code> method parameter is passed with a JSON as the body of the request.</p>