title: Spring boot - taking control of 404 Not Found
body: <p>I'm trying to figure out the simplest way to take control over the 404 Not Found handler of a basic Spring Boot RESTful service such as the example provided by Spring:</p>
  
  <p><a href="https://spring.io/guides/gs/rest-service/" rel="noreferrer">https://spring.io/guides/gs/rest-service/</a></p>
  
  <p>Rather than have it return the default Json output:</p>
  
  <pre><code>{
    "timestamp":1432047177086,
    "status":404,
    "error":"Not Found",
    "exception":"org.springframework.web.servlet.NoHandlerFoundException",
    "message":"No handler found for GET /aaa, ..."
  }
  </code></pre>
  
  <p>I'd like to provide my own Json output.</p>
  
  <p>By taking control of the <code>DispatcherServlet</code> and using <code>DispatcherServlet#setThrowExceptionIfNoHandlerFound(true)</code>, I was able to make it throw an exception in case of a 404 but I can't handle that exception through a <code>@ExceptionHandler</code>, like I would for a <code>MissingServletRequestParameterException</code>. Any idea why?</p>
  
  <p>Or is there a better approach than having a NoHandlerFoundException thrown and handled?</p>