title: spring rest Handling empty request body (400 Bad Request)
body: <p>I am developing a RESTful app using Spring4.
  I want to handle the case when a POST request contains no body.
  I wrote the following custom exception handler:</p>
  <pre><code>    @ControllerAdvice
      public class MyRestExceptionHandler {
       
        @ExceptionHandler
        @ResponseStatus(HttpStatus.BAD_REQUEST)
        public ResponseEntity&lt;MyErrorResponse&gt; handleJsonMappingException(JsonMappingException ex) {
            MyErrorResponse errorResponse = new MyErrorResponse(&quot;request has empty body&quot;);
            return new ResponseEntity&lt;MyErrorResponse&gt;(errorResponse, HttpStatus.BAD_REQUEST);
        }   
        @ExceptionHandler(Throwable.class)
        public ResponseEntity&lt;MyErrorResponse&gt; handleDefaultException(Throwable ex) {
          MyErrorResponse errorResponse = new MyErrorResponse(ex);
          return new ResponseEntity&lt;MyErrorResponse&gt;(errorResponse, HttpStatus.BAD_REQUEST);
        }
      }
      
       @RestController
       public class ContactRestController{
          @RequestMapping(path=&quot;/contact&quot;, method=RequestMethod.POST)
          public void save(@RequestBody ContactDTO contactDto) {...}
       } 
  </code></pre>
  <p>When it receives a POST with no body, these methods aren't called.
  Instead,
  the client gets a response with 400 BAD REQUEST HTTP status and empty body.
  Does anybody know how to handle it?</p>