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

public abstract Class ResponseEntityExceptionHandler

extends Object
implements MessageSourceAware
Class Inheritance
All Implemented Interfaces
org.springframework.context.MessageSourceAware, org.springframework.beans.factory.Aware
Imports
jakarta.servlet.http.HttpServletResponse, org.apache.commons.logging.Log, .LogFactory, org.springframework.beans.ConversionNotSupportedException, .TypeMismatchException, org.springframework.context.MessageSource, .MessageSourceAware, org.springframework.context.i18n.LocaleContextHolder, org.springframework.http.HttpHeaders, .HttpStatus, .HttpStatusCode, .ProblemDetail, .ResponseEntity, org.springframework.http.converter.HttpMessageNotReadableException, .HttpMessageNotWritableException, org.springframework.lang.Nullable, org.springframework.validation.BindException, org.springframework.validation.method.MethodValidationException, org.springframework.web.ErrorResponse, .ErrorResponseException, .HttpMediaTypeNotAcceptableException, .HttpMediaTypeNotSupportedException, .HttpRequestMethodNotSupportedException, org.springframework.web.bind.MethodArgumentNotValidException, .MissingPathVariableException, .MissingServletRequestParameterException, .ServletRequestBindingException, org.springframework.web.bind.annotation.ControllerAdvice, .ExceptionHandler, org.springframework.web.context.request.ServletWebRequest, .WebRequest, org.springframework.web.context.request.async.AsyncRequestTimeoutException, org.springframework.web.method.annotation.HandlerMethodValidationException, org.springframework.web.multipart.MaxUploadSizeExceededException, org.springframework.web.multipart.support.MissingServletRequestPartException, org.springframework.web.servlet.NoHandlerFoundException, org.springframework.web.servlet.resource.NoResourceFoundException, org.springframework.web.util.WebUtils

A class with an @ExceptionHandler method that handles all Spring MVC raised exceptions by returning a ResponseEntity with RFC 7807 formatted error details in the body.

Convenient as a base class of an @ControllerAdvice for global exception handling in an application. Subclasses can override individual methods that handle a specific exception, override handleExceptionInternal to override common handling of all exceptions, or override createResponseEntity to intercept the final step of creating the ResponseEntity from the selected HTTP status code, headers, and body.

Author
Rossen Stoyanchev
Since
3.2

Field Summary

Modifier and TypeField and Description
protected final Log
logger

Common logger for use in subclasses.

private MessageSource
public static final String
PAGE_NOT_FOUND_LOG_CATEGORY

Log category to use when no mapped handler is found for a request.

protected static final Log
pageNotFoundLogger

Specific logger to use when no mapped handler is found for a request.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected ProblemDetail

Returns:

the created ProblemDetail instance
createProblemDetail
(Exception
the exception being handled
ex
,
HttpStatusCode
the status to associate with the exception
status
,
String
default value for the "detail" field
defaultDetail
,
String
the code to use to look up the "detail" field through a MessageSource; if null then ErrorResponse#getDefaultDetailMessageCode(Class, String) is used to determine the default message code to use
detailMessageCode
,
Object[]
the arguments to go with the detailMessageCode
detailMessageArguments
,
WebRequest
the current request
request
)

Convenience method to create a ProblemDetail for any exception that doesn't implement ErrorResponse, also performing a MessageSource lookup for the "detail" field.

protected ResponseEntity<Object>

Returns:

the ResponseEntity instance to use
createResponseEntity
(Object
the body to use for the response
body
,
HttpHeaders
the headers to use for the response
headers
,
HttpStatusCode
the status code to use for the response
statusCode
,
WebRequest
the current request
request
)

Create the ResponseEntity to use from the given body, headers, and statusCode.

protected MessageSource
getMessageSource()

Get the MessageSource that this exception handler uses.

protected ResponseEntity<Object>

Returns:

a ResponseEntity for the response to use, possibly null when the response is already committed
handleAsyncRequestTimeoutException
(AsyncRequestTimeoutException
the exception to handle
ex
,
HttpHeaders
the headers to use for the response
headers
,
HttpStatusCode
the status code to use for the response
status
,
WebRequest
the current request
request
)

Customize the handling of AsyncRequestTimeoutException.

protected ResponseEntity<Object>

Returns:

a ResponseEntity for the response to use, possibly null when the response is already committed
handleBindException
(BindException
the exception to handle
ex
,
HttpHeaders
the headers to use for the response
headers
,
HttpStatusCode
the status code to use for the response
status
,
WebRequest
the current request
request
)
Deprecated for removal since 6.0. as of 6.0 since org.springframework.web.method.annotation.ModelAttributeMethodProcessor now raises the MethodArgumentNotValidException subclass instead.

Customize the handling of BindException.

protected ResponseEntity<Object>

Returns:

a ResponseEntity for the response to use, possibly null when the response is already committed
handleConversionNotSupported
(ConversionNotSupportedException
the exception to handle
ex
,
HttpHeaders
the headers to use for the response
headers
,
HttpStatusCode
the status code to use for the response
status
,
WebRequest
the current request
request
)

Customize the handling of ConversionNotSupportedException.

protected ResponseEntity<Object>

Returns:

a ResponseEntity for the response to use, possibly null when the response is already committed
handleErrorResponseException
(ErrorResponseException
the exception to handle
ex
,
HttpHeaders
the headers to use for the response
headers
,
HttpStatusCode
the status code to use for the response
status
,
WebRequest
the current request
request
)

Customize the handling of any ErrorResponseException.

public final ResponseEntity<Object>
handleException(Exception
the exception to handle
ex
,
WebRequest
the current request
request
)

Handle all exceptions raised within Spring MVC handling of the request.

protected ResponseEntity<Object>

Returns:

a ResponseEntity for the response to use, possibly null when the response is already committed
handleExceptionInternal
(Exception
the exception to handle
ex
,
Object
the body to use for the response
body
,
HttpHeaders
the headers to use for the response
headers
,
HttpStatusCode
the status code to use for the response
statusCode
,
WebRequest
the current request
request
)

Internal handler method that all others in this class delegate to, for common handling, and for the creation of a ResponseEntity.

protected ResponseEntity<Object>

Returns:

a ResponseEntity for the response to use, possibly null when the response is already committed
handleHandlerMethodValidationException
(HandlerMethodValidationException
the exception to handle
ex
,
HttpHeaders
the headers to be written to the response
headers
,
HttpStatusCode
the selected response status
status
,
WebRequest
the current request
request
)

Customize the handling of HandlerMethodValidationException.

protected ResponseEntity<Object>

Returns:

a ResponseEntity for the response to use, possibly null when the response is already committed
handleHttpMediaTypeNotAcceptable
(HttpMediaTypeNotAcceptableException
the exception to handle
ex
,
HttpHeaders
the headers to use for the response
headers
,
HttpStatusCode
the status code to use for the response
status
,
WebRequest
the current request
request
)

Customize the handling of HttpMediaTypeNotAcceptableException.

protected ResponseEntity<Object>

Returns:

a ResponseEntity for the response to use, possibly null when the response is already committed
handleHttpMediaTypeNotSupported
(HttpMediaTypeNotSupportedException
the exception to handle
ex
,
HttpHeaders
the headers to use for the response
headers
,
HttpStatusCode
the status code to use for the response
status
,
WebRequest
the current request
request
)

Customize the handling of HttpMediaTypeNotSupportedException.

protected ResponseEntity<Object>

Returns:

a ResponseEntity for the response to use, possibly null when the response is already committed
handleHttpMessageNotReadable
(HttpMessageNotReadableException
the exception to handle
ex
,
HttpHeaders
the headers to use for the response
headers
,
HttpStatusCode
the status code to use for the response
status
,
WebRequest
the current request
request
)

Customize the handling of HttpMessageNotReadableException.

protected ResponseEntity<Object>

Returns:

a ResponseEntity for the response to use, possibly null when the response is already committed
handleHttpMessageNotWritable
(HttpMessageNotWritableException
the exception to handle
ex
,
HttpHeaders
the headers to use for the response
headers
,
HttpStatusCode
the status code to use for the response
status
,
WebRequest
the current request
request
)

Customize the handling of HttpMessageNotWritableException.

protected ResponseEntity<Object>

Returns:

a ResponseEntity for the response to use, possibly null when the response is already committed
handleHttpRequestMethodNotSupported
(HttpRequestMethodNotSupportedException
the exception to handle
ex
,
HttpHeaders
the headers to use for the response
headers
,
HttpStatusCode
the status code to use for the response
status
,
WebRequest
the current request
request
)

Customize the handling of HttpRequestMethodNotSupportedException.

protected ResponseEntity<Object>

Returns:

a ResponseEntity for the response to use, possibly null when the response is already committed
handleMaxUploadSizeExceededException
(MaxUploadSizeExceededException
the exception to handle
ex
,
HttpHeaders
the headers to use for the response
headers
,
HttpStatusCode
the status code to use for the response
status
,
WebRequest
the current request
request
)

Customize the handling of any MaxUploadSizeExceededException.

protected ResponseEntity<Object>

Returns:

a ResponseEntity for the response to use, possibly null when the response is already committed
handleMethodArgumentNotValid
(MethodArgumentNotValidException
the exception to handle
ex
,
HttpHeaders
the headers to be written to the response
headers
,
HttpStatusCode
the selected response status
status
,
WebRequest
the current request
request
)

Customize the handling of MethodArgumentNotValidException.

protected ResponseEntity<Object>

Returns:

a ResponseEntity for the response to use, possibly null when the response is already committed
handleMethodValidationException
(MethodValidationException
the exception to handle
ex
,
HttpHeaders
the headers to use for the response
headers
,
HttpStatus
the status code to use for the response
status
,
WebRequest
the current request
request
)

Customize the handling of MethodValidationException.

protected ResponseEntity<Object>

Returns:

a ResponseEntity for the response to use, possibly null when the response is already committed
handleMissingPathVariable
(MissingPathVariableException
the exception to handle
ex
,
HttpHeaders
the headers to use for the response
headers
,
HttpStatusCode
the status code to use for the response
status
,
WebRequest
the current request
request
)

Customize the handling of MissingPathVariableException.

protected ResponseEntity<Object>

Returns:

a ResponseEntity for the response to use, possibly null when the response is already committed
handleMissingServletRequestParameter
(MissingServletRequestParameterException
the exception to handle
ex
,
HttpHeaders
the headers to use for the response
headers
,
HttpStatusCode
the status code to use for the response
status
,
WebRequest
the current request
request
)

Customize the handling of MissingServletRequestParameterException.

protected ResponseEntity<Object>

Returns:

a ResponseEntity for the response to use, possibly null when the response is already committed
handleMissingServletRequestPart
(MissingServletRequestPartException
the exception to handle
ex
,
HttpHeaders
the headers to use for the response
headers
,
HttpStatusCode
the status code to use for the response
status
,
WebRequest
the current request
request
)

Customize the handling of MissingServletRequestPartException.

protected ResponseEntity<Object>

Returns:

a ResponseEntity for the response to use, possibly null when the response is already committed
handleNoHandlerFoundException
(NoHandlerFoundException
the exception to handle
ex
,
HttpHeaders
the headers to use for the response
headers
,
HttpStatusCode
the status code to use for the response
status
,
WebRequest
the current request
request
)

Customize the handling of NoHandlerFoundException.

protected ResponseEntity<Object>

Returns:

a ResponseEntity for the response to use, possibly null when the response is already committed
handleNoResourceFoundException
(NoResourceFoundException
the exception to handle
ex
,
HttpHeaders
the headers to use for the response
headers
,
HttpStatusCode
the status code to use for the response
status
,
WebRequest
the current request
request
)

Customize the handling of NoResourceFoundException.

protected ResponseEntity<Object>

Returns:

a ResponseEntity for the response to use, possibly null when the response is already committed
handleServletRequestBindingException
(ServletRequestBindingException
the exception to handle
ex
,
HttpHeaders
the headers to use for the response
headers
,
HttpStatusCode
the status code to use for the response
status
,
WebRequest
the current request
request
)

Customize the handling of ServletRequestBindingException.

protected ResponseEntity<Object>

Returns:

a ResponseEntity for the response to use, possibly null when the response is already committed
handleTypeMismatch
(TypeMismatchException
the exception to handle
ex
,
HttpHeaders
the headers to use for the response
headers
,
HttpStatusCode
the status code to use for the response
status
,
WebRequest
the current request
request
)

Customize the handling of TypeMismatchException.

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 java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait