If an exception occurs during request rendering or is thrown from a request handler (such as an annotated @Controller
), DispatcherServlet
delegates authority to the HandlerExceptionResolver
bean chain to recognize the exception and provide alternative handling, which is usually an error message.
The following table lists the available HandlerExceptionResolver
implementations:
HandlerExceptionResolver |
Description |
---|---|
|
Map between exception class names and error representation names. Used to render error pages in a browser application. |
Recognizes exceptions thrown by Spring MVC and maps them to HTTP status codes. See also the alternative |
|
|
Recognizes exceptions with the |
|
Recognizes exceptions by calling a method marked with the |
Chain of resolvers
You can chain exception resolvers by declaring multiple HandlerExceptionResolver
beans in your Spring configuration and adjusting their order
properties as needed. The higher the order property, the further away the exception handler is located.
The HandlerExceptionResolver
contract specifies that it can return:
-
ModelAndView
indicating the error view. -
Empty
ModelAndView
if the exception was handled inside the resolver. -
null
if the exception remains unrecognized, for subsequent attempts made by resolvers, but if the exception ultimately persists, then it is allowed into the servlet container.
The MVC configuration automatically declares built-in handlers for default Spring MVC exceptions, for @ResponseStatus
annotated exceptions, and to support methods annotated with @ExceptionHandler
. This list can be modified or replaced.
Container error page
If the exception remains unrecognized by any HandlerExceptionResolver
and therefore continues to propagate, or if the response status is set to error (i.e. 4xx, 5xx), servlet containers can render a default error page at HTML. To customize the container's default error page, you can declare the error page display in web.xml
. The following example shows how to do this:
<error-page>
<location>/error</location>
</error-page>
Given the previous example, if an exception occurs or the response has an error status, the servlet container internally sends an ERROR to the configured URL (for example, /error
). This is all then processed by the DispatcherServlet
, by possibly mapping it to a @Controller
annotation, which can be implemented to return the name of an error view with a model, or to output a response in JSON format, as shown in the following example:
@RestController
public class ErrorController {
@RequestMapping(path = "/error")
public Map<String, Object> handle(HttpServletRequest request) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("status", request.getAttribute("javax.servlet.error.status_code"));
map.put("reason", request.getAttribute("javax.servlet.error.message"));
return map;
}
}
@RestController
class ErrorController {
@RequestMapping(path = ["/error"])
fun handle(request: HttpServletRequest): Map<String, Any> {
val map = HashMap<String, Any>()
map["status"] = request.getAttribute("javax.servlet.error.status_code")
map["reason"] = request.getAttribute("javax.servlet.error.message")
return map
}
}
WebApplicationInitializer
or a simple
web.xml
.
GO TO FULL VERSION