Top Description Fields Constructors Methods
org.springframework.web.servlet.mvc.annotation

public Class ResponseStatusExceptionResolver

extends AbstractHandlerExceptionResolver
implements MessageSourceAware
Class Inheritance
All Implemented Interfaces
org.springframework.context.MessageSourceAware, org.springframework.beans.factory.Aware
Imports
java.io.IOException, jakarta.servlet.http.HttpServletRequest, .HttpServletResponse, org.springframework.context.MessageSource, .MessageSourceAware, org.springframework.context.i18n.LocaleContextHolder, org.springframework.core.annotation.AnnotatedElementUtils, org.springframework.lang.Nullable, org.springframework.util.StringUtils, org.springframework.web.bind.annotation.ResponseStatus, org.springframework.web.server.ResponseStatusException, org.springframework.web.servlet.ModelAndView, org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver

A 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.

Authors
Arjen Poutsma, Rossen Stoyanchev, Sam Brannen
Since
3.0
See Also
ResponseStatus, ResponseStatusException

Field Summary

Modifier and TypeField and Description
private MessageSource
Inherited from org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver:
logger

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected ModelAndView
applyStatusAndReason(int
the HTTP status code
statusCode
,
String
the associated reason (may be null or empty)
reason
,
HttpServletResponse
current HTTP response
response
)

Apply the resolved status code and reason to the response.

protected ModelAndView
doResolveException(HttpServletRequest
current HTTP request
request
,
HttpServletResponse
current HTTP response
response
,
Object
the executed handler, or null if none chosen at the time of the exception (for example, if multipart resolution failed)
handler
,
Exception
the exception that got thrown during handler execution
ex
)

Implements abstract org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.doResolveException.

Actually resolve the given exception that got thrown during handler execution, returning a ModelAndView that represents a specific error page if appropriate.

protected ModelAndView

Returns:

an empty ModelAndView, i.e. exception resolved
resolveResponseStatus
(ResponseStatus
the @ResponseStatus annotation
responseStatus
,
HttpServletRequest
current HTTP request
request
,
HttpServletResponse
current HTTP response
response
,
Object
the executed handler, or null if none chosen at the time of the exception, e.g. if multipart resolution failed
handler
,
Exception
the exception
ex
)

Template method that handles the @ResponseStatus annotation.

protected ModelAndView

Returns:

an empty ModelAndView, i.e. exception resolved
resolveResponseStatusException
(ResponseStatusException
the exception
ex
,
HttpServletRequest
current HTTP request
request
,
HttpServletResponse
current HTTP response
response
,
Object
the executed handler, or null if none chosen at the time of the exception, e.g. if multipart resolution failed
handler
)

Template method that handles an ResponseStatusException.

public void
setMessageSource(MessageSource
message source to be used by this object
messageSource
)

Implements org.springframework.context.MessageSourceAware.setMessageSource.

Set the MessageSource that 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.

Inherited from org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver:
addMappedHandlerClassbuildLogMessagegetMappedHandlerClassesgetOrderhasHandlerMappingslogExceptionprepareResponsepreventCachingresolveExceptionsetMappedHandlerClassessetMappedHandlerPredicatesetMappedHandlerssetOrdersetPreventResponseCachingsetWarnLogCategoryshouldApplyTo

Field Detail

messageSourceback to summary
private MessageSource messageSource
Annotations
@Nullable

Constructor Detail

ResponseStatusExceptionResolverback to summary
public ResponseStatusExceptionResolver()

Method Detail

applyStatusAndReasonback to summary
protected ModelAndView applyStatusAndReason(int statusCode, String reason, 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) or HttpServletResponse#sendError(int, String) if there is a reason and then returns an empty ModelAndView.

Parameters
statusCode:int

the HTTP status code

reason:String

the associated reason (may be null or empty)

response:HttpServletResponse

current HTTP response

Since
5.0
doResolveExceptionback to summary
protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)

Implements abstract org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.doResolveException.

Doc from org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.doResolveException.

Actually resolve the given exception that got thrown during handler execution, returning a 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 resolver applies ("mappedHandlers" etc), so an implementation may simply proceed with its actual exception handling.

Parameters
request:HttpServletRequest

current HTTP request

response:HttpServletResponse

current HTTP response

handler:Object

the executed handler, or null if none chosen at the time of the exception (for example, if multipart resolution failed)

ex:Exception

the exception that got thrown during handler execution

Returns:ModelAndView

a corresponding ModelAndView to forward to, or null for default processing in the resolution chain

Annotations
@Override
@Nullable
resolveResponseStatusback to summary
protected ModelAndView resolveResponseStatus(ResponseStatus responseStatus, HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception

Template method that handles the @ResponseStatus annotation.

The default implementation delegates to applyStatusAndReason with the status code and reason from the annotation.

Parameters
responseStatus:ResponseStatus

the @ResponseStatus annotation

request:HttpServletRequest

current HTTP request

response:HttpServletResponse

current HTTP response

handler:Object

the executed handler, or null if none chosen at the time of the exception, e.g. if multipart resolution failed

ex:Exception

the exception

Returns:ModelAndView

an empty ModelAndView, i.e. exception resolved

resolveResponseStatusExceptionback to summary
protected ModelAndView resolveResponseStatusException(ResponseStatusException ex, HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception

Template method that handles an ResponseStatusException.

The default implementation applies the headers from ResponseStatusException#getHeaders() and delegates to applyStatusAndReason with the status code and reason from the exception.

Parameters
ex:ResponseStatusException

the exception

request:HttpServletRequest

current HTTP request

response:HttpServletResponse

current HTTP response

handler:Object

the executed handler, or null if none chosen at the time of the exception, e.g. if multipart resolution failed

Returns:ModelAndView

an empty ModelAndView, i.e. exception resolved

Since
5.0
setMessageSourceback to summary
public void setMessageSource(MessageSource messageSource)

Implements org.springframework.context.MessageSourceAware.setMessageSource.

Doc from org.springframework.context.MessageSourceAware.setMessageSource.

Set the MessageSource that 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.

Parameters
messageSource:MessageSource

message source to be used by this object

Annotations
@Override