title: Spring validation for RequestBody parameters bound to collections in Controller methods
body: <p>I have </p>
  
  <p><strong>An Entity:</strong></p>
  
  <pre><code>package org.ibp.soq;
  
  public class MyEntity {
  
      private String field1;
      private String field2;
  
      //..getters and setters
  
  }
  </code></pre>
  
  <p><strong>Validator for the entity:</strong></p>
  
  <pre><code>package org.ibp.soq;
  
  import org.springframework.stereotype.Component;
  import org.springframework.validation.Errors;
  import org.springframework.validation.Validator;
  
  @Component
  public class MyEntityValidator implements Validator {
  
      @Override
      public boolean supports(Class&lt;?&gt; clazz) {
          return MyEntity.class.equals(clazz);
      }
  
      @Override
      public void validate(Object target, Errors errors) {
          MyEntity myEntity = (MyEntity) target;
          // Logic to validate my entity
          System.out.print(myEntity);
      }
  
  }
  </code></pre>
  
  <p>and </p>
  
  <p><strong>The REST controller with bulk PUT method:</strong></p>
  
  <pre><code>package org.ibp.soq;
  
  import java.util.List;
  
  import javax.validation.Valid;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.web.bind.WebDataBinder;
  import org.springframework.web.bind.annotation.InitBinder;
  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.RestController;
  
  @RestController
  @RequestMapping("/myEntity")
  public class MyEntityRestResource {
  
      @Autowired
      private MyEntityValidator myEntityValidator;
  
      @InitBinder
      protected void initBinder(final WebDataBinder binder) {
          binder.addValidators(this.myEntityValidator);
      }
  
      @RequestMapping(method = RequestMethod.PUT)
      public void bulkCreate(@RequestBody @Valid List&lt;MyEntity&gt; myEntities) {
          // Logic to bulk create entities here.
          System.out.print(myEntities);
      }
  }
  </code></pre>
  
  <p>When I make a PUT request to this resource with following request body:</p>
  
  <pre><code>[
    {
      "field1": "AA",
      "field2": "11"
    },
  
    {
      "field1": "BB",
      "field2": "22"
    }
  ]
  </code></pre>
  
  <p>The error I get is:</p>
  
  <pre><code>"Invalid target for Validator [org.ibp.soq.MyEntityValidator@4eab617e]: [org.ibp.soq.MyEntity@21cebf1c, org.ibp.soq.MyEntity@c64d89b]"
  </code></pre>
  
  <p>I can understand that this is because <code>MyEntityValidator</code> "supports" single <code>MyEntity</code> validation, not validation for <code>ArrayList&lt;MyEntity&gt;</code>.</p>
  
  <p><code>MyEntityValidator</code> works perfectly if I have single <code>MyEntity</code> object in request body and a corresponding controller method with <code>@RequestBody @Valid MyEntity myEntity</code> parameter. </p>
  
  <p>How can the validator setup I have used, be extended for supporting validation of collection of <code>MyEntity</code>'s ?</p>