Class ResponseStatusExceptionResolver
- All Implemented Interfaces:
Aware, MessageSourceAware, Ordered, HandlerExceptionResolver
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.
- Since:
- 3.0
- Author:
- Arjen Poutsma, Rossen Stoyanchev, Sam Brannen
- See Also:
-
Field Summary
Fields inherited from class AbstractHandlerExceptionResolver
loggerFields inherited from interface Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected ModelAndViewapplyStatusAndReason(int statusCode, @Nullable String reason, jakarta.servlet.http.HttpServletResponse response) Apply the resolved status code and reason to the response.protected @Nullable ModelAndViewdoResolveException(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, @Nullable Object handler, Exception ex) Actually resolve the given exception that got thrown during handler execution, returning aModelAndViewthat represents a specific error page if appropriate.protected ModelAndViewresolveResponseStatus(ResponseStatus responseStatus, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, @Nullable Object handler, Exception ex) Template method that handles the@ResponseStatusannotation.protected ModelAndViewresolveResponseStatusException(ResponseStatusException ex, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, @Nullable Object handler) Template method that handles anResponseStatusException.voidsetMessageSource(@Nullable MessageSource messageSource) Set theMessageSourcethat this object runs in.Methods inherited from class AbstractHandlerExceptionResolver
addMappedHandlerClass, buildLogMessage, getMappedHandlerClasses, getOrder, hasHandlerMappings, logException, prepareResponse, preventCaching, resolveException, setMappedHandlerClasses, setMappedHandlerPredicate, setMappedHandlers, setOrder, setPreventResponseCaching, setWarnLogCategory, shouldApplyTo
-
Constructor Details
-
ResponseStatusExceptionResolver
public ResponseStatusExceptionResolver()
-
-
Method Details
-
setMessageSource
Description copied from interface:MessageSourceAwareSet theMessageSourcethat this object runs in.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.
- Specified by:
setMessageSourcein interfaceMessageSourceAware- Parameters:
messageSource- message source to be used by this object
-
doResolveException
protected @Nullable ModelAndView doResolveException(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, @Nullable Object handler, Exception ex) Description copied from class:AbstractHandlerExceptionResolverActually resolve the given exception that got thrown during handler execution, returning aModelAndViewthat 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 resolver applies ("mappedHandlers" etc), so an implementation may simply proceed with its actual exception handling.
- Specified by:
doResolveExceptionin classAbstractHandlerExceptionResolver- Parameters:
request- current HTTP requestresponse- current HTTP responsehandler- the executed handler, ornullif none chosen at the time of the exception (for example, if multipart resolution failed)ex- the exception that got thrown during handler execution- Returns:
- a corresponding
ModelAndViewto forward to, ornullfor default processing in the resolution chain
-
resolveResponseStatus
protected ModelAndView resolveResponseStatus(ResponseStatus responseStatus, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, @Nullable Object handler, Exception ex) throws Exception Template method that handles the@ResponseStatusannotation.The default implementation delegates to
applyStatusAndReason(int, String, HttpServletResponse)with the status code and reason from the annotation.- Parameters:
responseStatus- the@ResponseStatusannotationrequest- current HTTP requestresponse- current HTTP responsehandler- the executed handler, ornullif none chosen at the time of the exception, for example, if multipart resolution failedex- the exception- Returns:
- an empty ModelAndView, i.e. exception resolved
- Throws:
Exception
-
resolveResponseStatusException
protected ModelAndView resolveResponseStatusException(ResponseStatusException ex, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, @Nullable Object handler) throws Exception Template method that handles anResponseStatusException.The default implementation applies the headers from
ResponseStatusException.getHeaders()and delegates toapplyStatusAndReason(int, String, HttpServletResponse)with the status code and reason from the exception.- Parameters:
ex- the exceptionrequest- current HTTP requestresponse- current HTTP responsehandler- the executed handler, ornullif none chosen at the time of the exception, for example, if multipart resolution failed- Returns:
- an empty ModelAndView, i.e. exception resolved
- Throws:
Exception- Since:
- 5.0
-
applyStatusAndReason
protected ModelAndView applyStatusAndReason(int statusCode, @Nullable String reason, jakarta.servlet.http.HttpServletResponse response) throws IOException Apply the resolved status code and reason to the response.The default implementation sends a response error using
HttpServletResponse.sendError(int)orHttpServletResponse.sendError(int, String)if there is a reason and then returns an empty ModelAndView.- Parameters:
statusCode- the HTTP status codereason- the associated reason (may benullor empty)response- current HTTP response- Throws:
IOException- Since:
- 5.0
-