title: Why are @Valid annotations ignored on MultipartFile objects in Spring?
body: <p>This is a spin-off from a previous question, <a href="https://stackoverflow.com/questions/51379247/spring-validator-class-is-ignored-by-controller/51381362">Spring validator class is ignored by controller</a>. I'm struggling to make my controller accept a <code>MultipartFile</code> object and make Spring bind a <code>Validator</code> to this object. Succintly, this does <em>not</em> work:</p>
  
  <pre><code>@RequestMapping("/api")
  @RestController
  public class Controller{
  
      private FileValidator fileValidator;
  
      @Autowired
      public myObjectController(FileValidator fileValidator){
          this.fileValidator = fileValidator;
      }
  
      @InitBinder("file")
      public void initFileBinder(WebDataBinder binder) {
          binder.setValidator(this.fileValidator);
      }
  
      @PostMapping("myObject")
      @ResponseStatus(HttpStatus.CREATED)
      @ResponseBody
      public MyObject createMyObject(
          @RequestPart("file") @Valid MultipartFile file
      ){
          return null; //201 CREATED every time
      }
  }
  
  
  
  public class FileValidator implements Validator{
  
      public boolean supports(Class&lt;?&gt; aClass) {
          //Breakpoint on line below is never triggered.
          return MultipartFile.class.isAssignableFrom(aClass); 
      }
  
      public void validate(Object o, Errors e) {
          //validation happening here
      }
  }
  </code></pre>
  
  <p>I'm not the first person on the world wide web to face a similar problem. </p>
  
  <p><a href="http://javabycode.com/spring-framework-tutorial/spring-mvc-tutorial/spring-mvc-file-upload-validation-example.html" rel="nofollow noreferrer">http://javabycode.com/spring-framework-tutorial/spring-mvc-tutorial/spring-mvc-file-upload-validation-example.html</a></p>
  
  <p><a href="https://stackoverflow.com/a/7256042/4640960">https://stackoverflow.com/a/7256042/4640960</a></p>
  
  <p><a href="https://memorynotfound.com/spring-mvc-file-upload-example-validator/" rel="nofollow noreferrer">https://memorynotfound.com/spring-mvc-file-upload-example-validator/</a></p>
  
  <p><a href="https://stackoverflow.com/a/13085988/4640960">https://stackoverflow.com/a/13085988/4640960</a></p>
  
  <p><a href="https://hackinghorse.blogspot.com/2016/08/devnote-uploading-multipart-file-with.html" rel="nofollow noreferrer">https://hackinghorse.blogspot.com/2016/08/devnote-uploading-multipart-file-with.html</a></p>
  
  <p>All examples solve my problem my wrapping the <code>MultipartFile</code> in a very dumb wrapper class in order for the <code>@Valid</code> annotation to work. It seems as if this is necessary, but <em>why</em> is this necessary? I must emphasize that this particular question is not about working around my technical problem, but to understand what it is about <code>MultipartFile</code> that makes these objects ignored by the <code>@Valid</code> annotation.</p>