title: Spring Boot Validation Respond with JSON
body: <p>I'm still new to Spring in general and I'm trying to use Spring boot.  I have a (hopefully) quick question. I'm trying to build a ReSTful service which will return JSON.  I have followed the <a href="http://spring.io/guides/gs/rest-service/" rel="nofollow">Building a RESTful Web Service Guide</a> and can successfully return JSON. I've integrated JPA into my web service so that my data is backed by a database. </p>
  
  <p>Now, I need to make a route in which, users can create an object and I would like the object to be validated.  I've followed the <a href="http://spring.io/guides/gs/validating-form-input/" rel="nofollow">Validation Form Input Guide</a> but I'm not really trying to create a service that serves up web content.  What I want is, whenever a validation error occurs, to return my own custom JSON.  Thus far, I haven't been able to find any resources for making this happen though I've tried following <a href="http://www.petrikainulainen.net/programming/spring-framework/spring-from-the-trenches-adding-validation-to-a-rest-api/" rel="nofollow">Petri's Sweet Rest API Guide</a> which I've found helpful on multiple occasions but doesn't seem to quite work in this scenario. I'm using <code>hibernate-validator:5.0.1.Final</code> and hibernate for the following. </p>
  
  <pre><code>@Entity
  @Table(name = "PEOPLE")
  public class Person{
  
      @Id
      @Column(unique = true)
      @GeneratedValue(strategy = GenerationType.AUTO)
      private long id;
  
      @Min(18)
      private long age;
      private String name;
  
      //Necessary for JPA
      protected Person() {}
  
      public Person(long age, String name) {
          this.age = age;
          this.name = name;
      }
  
      // Getters Omitted
  }
  </code></pre>
  
  <p>Then my PersonController:</p>
  
  <pre><code>@Controller
  public class PersonController {
  
      @RequestMapping(value="person/", method=RequestMethod.POST)
      @ResponseBody
      public ResponseEntity&lt;Person&gt; create(@Valid @RequestBody Person person) {
          // Create in DB and return
      }
  } 
  </code></pre>
  
  <p>This works in the most strict way, in that, if you send garbage JSON to this route, it will return a 400 which is pretty nice. But the body of the response is an HTML page which is not as nice. So, my question is, is there some way to catch a validation error?  I've tried adding the following to my Controller but with no success:</p>
  
  <pre><code>@ExceptionHandler(MethodArgumentNotValidException.class)
  public ResponseEntity handleError(MethodArgumentNotValidException ex) {
      //generate my own error message
      return new ResponseEntity(customErrorClass, HttpStatus.BAD_GATEWAY);
  }
  </code></pre>
  
  <p>I'm aware Bad Gateway is not a valid return code, but I used it just to prove that the Exception handler is never called. When I POST to my rest service, I still see 400 Bad Request + HTML. I would assume that there is some sensible default that I can override but I can't seem to figure out where it is.  I've tried googling and searching stackoverflow to no luck. </p>
  
  <p>Thanks in advance.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>If I modify the Controller to include a <code>BindingResult</code> in the method signature:</p>
  
  <pre><code>@Controller
  public class PersonController {
  
      @RequestMapping(value="person/", method=RequestMethod.POST)
      @ResponseBody
      public ResponseEntity&lt;Person&gt; create(@Valid @RequestBody Person person, BindingResult bindingResult) {
          if(bindingResult.hasErrors()){
              //handle errors and return
          } else {
          // Create in DB and return
          }
      }
  } 
  </code></pre>
  
  <p>I can get it to work. (<em>Note I also had to add the jasper-el jar to my dependencies</em>) I had tried this before and didn't get it to work but the reason is not intuitive.  I was posting with the following JSON: <code>{ "id" : "foo", "age": 22, "name" : "James Bond" }</code>  According to the <a href="http://www.json.org/" rel="nofollow">JSON Spec</a>, this is valid JSON. Obviously, my Person Model cannot cast the String "foo" to a long.  I won't go into whether or not the error should be a 400 or not, my new question is this:  How can I catch this 400? </p>