title: How to validate a Map<String, String> using Spring Validator programmatically
body: <p>I have a Map that I receive from a browser redirection back from a third party to my Spring Controller as below - </p>
  
  <pre><code>@RequestMapping(value = "/capture", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
      public void capture(@RequestParam
      final Map&lt;String, String&gt; response)
      {
          // TODO : perform validations first.
          captureResponse(response);
      }
  </code></pre>
  
  <p>Before using this payload, I need to do non-trivial validation, involving first checking for non-null values of a map, and then using those values in a checksum validation. So, I would like to validate my payload programmatically using the Spring Validator interface. However, I could not find any validator example for validating a Map.</p>
  
  <p>For validating a Java Object, I understand how a Validator is invoked by passing the object and a BeanPropertyBindingResult to contain the errors to the Validator as below - </p>
  
  <pre><code>final Errors errors = new BeanPropertyBindingResult(object, objectName);
  myValidator.validate(object, errors);
  if (errors.hasErrors())
  {
      throw new MyWebserviceValidationException(errors);
  }
  </code></pre>
  
  <p>For a Map, I can see that there is a <code>MapBindingResult</code> class that extends <code>AbstractBindingResult</code>. Should I simply use it, and pass my map in the <code>Object object</code> and in the validator cast it back to a <code>Map</code>? Also, how would the Validator method of <code>supports(final Class&lt;?&gt; clazz)</code> be implemented in my validator? Would it simply be like below code snippet where there can only be one validator supporting this generic class of HashMap? Somehow doesn't feel right. (Although this does not matter to me as I will be injecting my validator and use it directly and not through a validator registry, but still curious.)</p>
  
  <pre><code>@Override
  public boolean supports(final Class&lt;?&gt; clazz)
  {
      return HashMap.class.equals(clazz);
  }
  </code></pre>
  
  <p>Since, there is a MapBindingResult, I'm positive that Spring must be supporting Maps for validation, would like to know how. So would like to know if this is the way to go, or am I heading in the wrong direction and there is a better way of doing this.</p>
  
  <p>Please note I would like to do this programmatically and not via annotations.</p>