Package org.springframework.web.servlet.mvc.annotation

# Class ResponseStatusExceptionResolver

java.lang.Object
org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver
org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver

All Implemented Interfaces:
  `org.springframework.beans.factory.Aware`, `org.springframework.context.MessageSourceAware`, `org.springframework.core.Ordered`, `HandlerExceptionResolver`

public class ResponseStatusExceptionResolver extends AbstractHandlerExceptionResolver implements org.springframework.context.MessageSourceAware
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`.

Since:
  3.0
Author:
  Arjen Poutsma, Rossen Stoyanchev, Sam Brannen
See Also:
  - `ResponseStatus`
  - `ResponseStatusException`

## Field Summary

### Fields inherited from class org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver

`logger`

### Fields inherited from interface org.springframework.core.Ordered

`HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE`

## Constructor Summary

Constructors
Constructor
Description
`ResponseStatusExceptionResolver()`

## Method Summary

Modifier and Type
Method
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(org.springframework.web.bind.annotation.ResponseStatus responseStatus, HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)`
Template method that handles the `@ResponseStatus` annotation.
`protected ModelAndView`
`resolveResponseStatusException(org.springframework.web.server.ResponseStatusException ex, HttpServletRequest request, HttpServletResponse response, Object handler)`
Template method that handles an `ResponseStatusException`.
`void`
`setMessageSource(org.springframework.context.MessageSource messageSource)`

### Methods inherited from class org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver

`addMappedHandlerClass, buildLogMessage, getMappedHandlerClasses, getOrder, hasHandlerMappings, logException, prepareResponse, preventCaching, resolveException, setMappedHandlerClasses, setMappedHandlerPredicate, setMappedHandlers, setOrder, setPreventResponseCaching, setWarnLogCategory, shouldApplyTo`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### ResponseStatusExceptionResolver

public ResponseStatusExceptionResolver()

## Method Details

### setMessageSource

public void setMessageSource(@Nullable org.springframework.context.MessageSource messageSource)

Specified by:
  `setMessageSource` in interface `org.springframework.context.MessageSourceAware`

### doResolveException

@Nullable protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, @Nullable Object handler, Exception ex)
Description copied from class: `AbstractHandlerExceptionResolver`
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.

Specified by:
  `doResolveException` in class `AbstractHandlerExceptionResolver`
Parameters:
  `request` - current HTTP request
  `response` - current HTTP response
  `handler` - 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 execution
Returns:
  a corresponding `ModelAndView` to forward to, or `null` for default processing in the resolution chain

### resolveResponseStatus

protected ModelAndView resolveResponseStatus(org.springframework.web.bind.annotation.ResponseStatus responseStatus, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler, Exception ex) throws Exception
Template method that handles the `@ResponseStatus` annotation.

The default implementation delegates to `applyStatusAndReason(int, java.lang.String, jakarta.servlet.http.HttpServletResponse)` with the status code and reason from the annotation.

Parameters:
  `responseStatus` - the `@ResponseStatus` annotation
  `request` - current HTTP request
  `response` - current HTTP response
  `handler` - the executed handler, or `null` if none chosen at the time of the exception, for example, if multipart resolution failed
  `ex` - the exception
Returns:
  an empty ModelAndView, i.e. exception resolved
Throws:
  `Exception`

### resolveResponseStatusException

protected ModelAndView resolveResponseStatusException(org.springframework.web.server.ResponseStatusException ex, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws Exception
Template method that handles an `ResponseStatusException`.

The default implementation applies the headers from `ResponseStatusException.getHeaders()` and delegates to `applyStatusAndReason(int, java.lang.String, jakarta.servlet.http.HttpServletResponse)` with the status code and reason from the exception.

Parameters:
  `ex` - the exception
  `request` - current HTTP request
  `response` - current HTTP response
  `handler` - the executed handler, or `null` if 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, 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` - the HTTP status code
  `reason` - the associated reason (may be `null` or empty)
  `response` - current HTTP response
Throws:
  `IOException`
Since:
  5.0