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:
https://spring.io/guides/gs/rest-service/
Rather than have it return the default Json output:
{
"timestamp":1432047177086,
"status":404,
"error":"Not Found",
"exception":"org.springframework.web.servlet.NoHandlerFoundException",
"message":"No handler found for GET /aaa, ..."
}
I'd like to provide my own Json output.
By taking control of the DispatcherServlet
and using DispatcherServlet#setThrowExceptionIfNoHandlerFound(true)
, I was able to make it throw an exception in case of a 404 but I can't handle that exception through a @ExceptionHandler
, like I would for a MissingServletRequestParameterException
. Any idea why?
Or is there a better approach than having a NoHandlerFoundException thrown and handled?
See Question&Answers more detail:os