public class ResponseStatusExceptionResolver extends AbstractHandlerExceptionResolver implements MessageSourceAware
HandlerExceptionResolver
that uses the @ResponseStatus
annotation to map exceptions to HTTP status codes.
This exception resolver is enabled by default in the
DispatcherServlet
and the MVC Java config and the MVC namespace.
As of 4.2 this resolver also looks recursively for @ResponseStatus
present on cause exceptions, and as of 4.2.2 this resolver supports
attribute overrides for @ResponseStatus
in custom composed annotations.
As of 5.0 this resolver also supports ResponseStatusException
.
ResponseStatus
,
ResponseStatusException
logger
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
Constructor and Description |
---|
ResponseStatusExceptionResolver() |
Modifier and Type | Method and Description |
---|---|
protected ModelAndView |
applyStatusAndReason(int statusCode,
String reason,
HttpServletResponse response)
Apply the resolved status code and reason to the response.
|
protected ModelAndView |
doResolveException(HttpServletRequest request,
HttpServletResponse response,
Object handler,
Exception ex)
Actually resolve the given exception that got thrown during handler execution,
returning a
ModelAndView that represents a specific error page if appropriate. |
protected ModelAndView |
resolveResponseStatus(ResponseStatus responseStatus,
HttpServletRequest request,
HttpServletResponse response,
Object handler,
Exception ex)
Template method that handles the
@ResponseStatus annotation. |
protected ModelAndView |
resolveResponseStatusException(ResponseStatusException ex,
HttpServletRequest request,
HttpServletResponse response,
Object handler)
Template method that handles an
ResponseStatusException . |
void |
setMessageSource(MessageSource messageSource)
Set the MessageSource that this object runs in.
|
buildLogMessage, getOrder, logException, prepareResponse, preventCaching, resolveException, setMappedHandlerClasses, setMappedHandlers, setOrder, setPreventResponseCaching, setWarnLogCategory, shouldApplyTo
public void setMessageSource(MessageSource messageSource)
MessageSourceAware
Invoked after population of normal bean properties but before an init callback like InitializingBean's afterPropertiesSet or a custom init-method. Invoked before ApplicationContextAware's setApplicationContext.
setMessageSource
in interface MessageSourceAware
messageSource
- message source to be used by this object@Nullable protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, @Nullable Object handler, Exception ex)
AbstractHandlerExceptionResolver
ModelAndView
that represents a specific error page if appropriate.
May be overridden in subclasses, in order to apply specific exception checks. Note that this template method will be invoked after checking whether this resolved applies ("mappedHandlers" etc), so an implementation may simply proceed with its actual exception handling.
doResolveException
in class AbstractHandlerExceptionResolver
request
- current HTTP requestresponse
- current HTTP responsehandler
- the executed handler, or null
if none chosen at the time
of the exception (for example, if multipart resolution failed)ex
- the exception that got thrown during handler executionModelAndView
to forward to,
or null
for default processing in the resolution chainprotected ModelAndView resolveResponseStatus(ResponseStatus responseStatus, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler, Exception ex) throws Exception
@ResponseStatus
annotation.
The default implementation delegates to applyStatusAndReason(int, java.lang.String, javax.servlet.http.HttpServletResponse)
with the status code and reason from the annotation.
responseStatus
- the @ResponseStatus
annotationrequest
- current HTTP requestresponse
- current HTTP responsehandler
- the executed handler, or null
if none chosen at the
time of the exception, e.g. if multipart resolution failedex
- the exceptionException
protected ModelAndView resolveResponseStatusException(ResponseStatusException ex, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws Exception
ResponseStatusException
.
The default implementation applies the headers from
ResponseStatusException.getResponseHeaders()
and delegates to
applyStatusAndReason(int, java.lang.String, javax.servlet.http.HttpServletResponse)
with the status code and reason from the
exception.
ex
- the exceptionrequest
- current HTTP requestresponse
- current HTTP responsehandler
- the executed handler, or null
if none chosen at the
time of the exception, e.g. if multipart resolution failedException
protected ModelAndView applyStatusAndReason(int statusCode, @Nullable String reason, HttpServletResponse response) throws IOException
The default implementation sends a response error using
HttpServletResponse.sendError(int)
or
HttpServletResponse.sendError(int, String)
if there is a reason
and then returns an empty ModelAndView.
statusCode
- the HTTP status codereason
- the associated reason (may be null
or empty)response
- current HTTP responseIOException