title: Data Binding Error Handling in Spring MVC
body: <p>I have a question about data binding in Spring MVC.</p>
  
  <p>I have a Controller which accepts a JSON request in the form of @RequestBody. I have all the JSR 303 validations in place and it works like a charm.</p>
  
  <ul>
  <li><p>JSON Request</p>
  
  <pre><code>public class TestJSONRequest {
  
      @Size(min=10,message="{invalid.demo.size}")
      String demo;
  
      int code;
  }
  </code></pre></li>
  <li><p>Controller</p>
  
  <pre><code>@Controller
  @RequestMapping("/test")
  public class TestController {
  
      public void testEntry(@RequestBody TestJSONRequest jsonRequest,ModelMap map)
  
        Set&lt;ConstraintViolation&lt;TestJSONRequest&gt;&gt; violationList = validator.val(jsonRequest);
        ....
        ....
        TestJSONResponse response = // Do complex Logic.
        modelMap.addattribute("TestJSONResponse",response);
      }
  }
  </code></pre></li>
  </ul>
  
  <p>But JSR 303 validations kick in once the incoming JSON data is bound to the Request object.</p>
  
  <p>If I send <code>ab</code> in the <strong>code</strong> field of the input JSON request, binding would itself fail.</p>
  
  <p>How do I handle that?</p>
  
  <p>I want to catch those data binding errors and do some kind of generalized error handling in my controller.</p>
  
  <p>Could you please help me out on this?</p>
  
  <p>P.S - I am using Spring 3.0.3</p>