title: How to throw an exception back in JSON in Spring Boot
body: <p>I have a Request Mapping - </p>
  
  <pre><code>  @RequestMapping("/fetchErrorMessages")
    public @ResponseBody int fetchErrorMessages(@RequestParam("startTime") String startTime,@RequestParam("endTime") String endTime) throws Exception
    {
        if(SanityChecker.checkDateSanity(startTime)&amp;&amp;SanityChecker.checkDateSanity(endTime))
        {
            return 0;
        }
        else
        {
            throw new NotFoundException("Datetime is invalid");
        }
    }
  </code></pre>
  
  <p>If the startTime and endTime are invalid, I want to throw a 500 error but return the exception string in JSON. However, I get a HTML Page instead saying </p>
  
  <blockquote>
    <p><strong>Whitelabel Error Page</strong></p>
    
    <p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p>
    
    <p>Wed Dec 20 10:49:37 IST 2017<br/>
    There was an unexpected error (type=Internal Server Error, status=500).<br/>
    Datetime is invalid</p>
  </blockquote>
  
  <p>I instead wanted to return 500 with a JSON </p>
  
  <pre><code>{"error":"Date time format is invalid"}
  </code></pre>
  
  <p>How do I go about this?</p>