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

# Class ExceptionHandlerExceptionResolver

java.lang.Object
org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver
org.springframework.web.servlet.handler.AbstractHandlerMethodExceptionResolver
org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver

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

public class ExceptionHandlerExceptionResolver extends AbstractHandlerMethodExceptionResolver implements org.springframework.context.ApplicationContextAware, org.springframework.beans.factory.InitializingBean
An `AbstractHandlerMethodExceptionResolver` that resolves exceptions through `@ExceptionHandler` methods.

Support for custom argument and return value types can be added via `setCustomArgumentResolvers(java.util.List<org.springframework.web.method.support.HandlerMethodArgumentResolver>)` and `setCustomReturnValueHandlers(java.util.List<org.springframework.web.method.support.HandlerMethodReturnValueHandler>)`. Or alternatively to re-configure all argument and return value types use `setArgumentResolvers(java.util.List<org.springframework.web.method.support.HandlerMethodArgumentResolver>)` and `setReturnValueHandlers(List)`.

Since:
  3.1
Author:
  Rossen Stoyanchev, Juergen Hoeller, Sebastien Deleuze, Brian Clozel

## 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
`ExceptionHandlerExceptionResolver()`

## Method Summary

Modifier and Type
Method
Description
`void`
`afterPropertiesSet()`
`protected ModelAndView`
`doResolveHandlerMethodException(HttpServletRequest request, HttpServletResponse response, org.springframework.web.method.HandlerMethod handlerMethod, Exception exception)`
Find an `@ExceptionHandler` method and invoke it to handle the raised exception.
`org.springframework.context.ApplicationContext`
`getApplicationContext()`
`org.springframework.web.method.support.HandlerMethodArgumentResolverComposite`
`getArgumentResolvers()`
Return the configured argument resolvers, or possibly `null` if not initialized yet via `afterPropertiesSet()`.
`org.springframework.web.accept.ContentNegotiationManager`
`getContentNegotiationManager()`
Return the configured `ContentNegotiationManager`.
`List<org.springframework.web.method.support.HandlerMethodArgumentResolver>`
`getCustomArgumentResolvers()`
Return the custom argument resolvers, or `null`.
`List<org.springframework.web.method.support.HandlerMethodReturnValueHandler>`
`getCustomReturnValueHandlers()`
Return the custom return value handlers, or `null`.
`protected List<org.springframework.web.method.support.HandlerMethodArgumentResolver>`
`getDefaultArgumentResolvers()`
Return the list of argument resolvers to use including built-in resolvers and custom resolvers provided via `setCustomArgumentResolvers(java.util.List<org.springframework.web.method.support.HandlerMethodArgumentResolver>)`.
`protected List<org.springframework.web.method.support.HandlerMethodReturnValueHandler>`
`getDefaultReturnValueHandlers()`
Return the list of return value handlers to use including built-in and custom handlers provided via `setReturnValueHandlers(java.util.List<org.springframework.web.method.support.HandlerMethodReturnValueHandler>)`.
`List<org.springframework.web.ErrorResponse.Interceptor>`
`getErrorResponseInterceptors()`
Return the `configured` `ErrorResponse.Interceptor`'s.
`Map<org.springframework.web.method.ControllerAdviceBean,org.springframework.web.method.annotation.ExceptionHandlerMethodResolver>`
`getExceptionHandlerAdviceCache()`
Return an unmodifiable Map with the `@ControllerAdvice` beans discovered in the ApplicationContext.
`protected ServletInvocableHandlerMethod`
`getExceptionHandlerMethod(org.springframework.web.method.HandlerMethod handlerMethod, Exception exception, org.springframework.web.context.request.ServletWebRequest webRequest)`
Find an `@ExceptionHandler` method for the given exception.
`List<org.springframework.http.converter.HttpMessageConverter<?>>`
`getMessageConverters()`
Return the configured message body converters.
`org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite`
`getReturnValueHandlers()`
Return the configured handlers, or possibly `null` if not initialized yet via `afterPropertiesSet()`.
`protected boolean`
`hasGlobalExceptionHandlers()`
Whether this resolver has global exception handlers, for example, not declared in the same class as the `HandlerMethod` that raised the exception and therefore can apply to any handler.
`void`
`setApplicationContext(org.springframework.context.ApplicationContext applicationContext)`
`void`
`setArgumentResolvers(List<org.springframework.web.method.support.HandlerMethodArgumentResolver> argumentResolvers)`
Configure the complete list of supported argument types thus overriding the resolvers that would otherwise be configured by default.
`void`
`setContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager contentNegotiationManager)`
Set the `ContentNegotiationManager` to use to determine requested media types.
`void`
`setCustomArgumentResolvers(List<org.springframework.web.method.support.HandlerMethodArgumentResolver> argumentResolvers)`
Provide resolvers for custom argument types.
`void`
`setCustomReturnValueHandlers(List<org.springframework.web.method.support.HandlerMethodReturnValueHandler> returnValueHandlers)`
Provide handlers for custom return value types.
`void`
`setErrorResponseInterceptors(List<org.springframework.web.ErrorResponse.Interceptor> interceptors)`
Configure a list of `ErrorResponse.Interceptor`'s to apply when rendering an RFC 9457 `ProblemDetail` error response.
`void`
`setMessageConverters(List<org.springframework.http.converter.HttpMessageConverter<?>> messageConverters)`
Set the message body converters to use.
`void`
`setResponseBodyAdvice(List<ResponseBodyAdvice<?>> responseBodyAdvice)`
Add one or more components to be invoked after the execution of a controller method annotated with `@ResponseBody` or returning `ResponseEntity` but before the body is written to the response with the selected `HttpMessageConverter`.
`void`
`setReturnValueHandlers(List<org.springframework.web.method.support.HandlerMethodReturnValueHandler> returnValueHandlers)`
Configure the complete list of supported return value types thus overriding handlers that would otherwise be configured by default.
`protected boolean`
`shouldApplyTo(HttpServletRequest request, Object handler)`
Checks if the handler is a `HandlerMethod` or the resolver has global exception handlers and then delegates to the base class implementation of `#shouldApplyTo` passing the bean of the `HandlerMethod` if necessary.

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

`doResolveException`

### 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`

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

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

## Constructor Details

### ExceptionHandlerExceptionResolver

public ExceptionHandlerExceptionResolver()

## Method Details

### setCustomArgumentResolvers

public void setCustomArgumentResolvers(@Nullable List<org.springframework.web.method.support.HandlerMethodArgumentResolver> argumentResolvers)
Provide resolvers for custom argument types. Custom resolvers are ordered after built-in ones. To override the built-in support for argument resolution use `setArgumentResolvers(java.util.List<org.springframework.web.method.support.HandlerMethodArgumentResolver>)` instead.

### getCustomArgumentResolvers

@Nullable public List<org.springframework.web.method.support.HandlerMethodArgumentResolver> getCustomArgumentResolvers()
Return the custom argument resolvers, or `null`.

### setArgumentResolvers

public void setArgumentResolvers(@Nullable List<org.springframework.web.method.support.HandlerMethodArgumentResolver> argumentResolvers)
Configure the complete list of supported argument types thus overriding the resolvers that would otherwise be configured by default.

### getArgumentResolvers

@Nullable public org.springframework.web.method.support.HandlerMethodArgumentResolverComposite getArgumentResolvers()
Return the configured argument resolvers, or possibly `null` if not initialized yet via `afterPropertiesSet()`.

### setCustomReturnValueHandlers

public void setCustomReturnValueHandlers(@Nullable List<org.springframework.web.method.support.HandlerMethodReturnValueHandler> returnValueHandlers)
Provide handlers for custom return value types. Custom handlers are ordered after built-in ones. To override the built-in support for return value handling use `setReturnValueHandlers(java.util.List<org.springframework.web.method.support.HandlerMethodReturnValueHandler>)`.

### getCustomReturnValueHandlers

@Nullable public List<org.springframework.web.method.support.HandlerMethodReturnValueHandler> getCustomReturnValueHandlers()
Return the custom return value handlers, or `null`.

### setReturnValueHandlers

public void setReturnValueHandlers(@Nullable List<org.springframework.web.method.support.HandlerMethodReturnValueHandler> returnValueHandlers)
Configure the complete list of supported return value types thus overriding handlers that would otherwise be configured by default.

### getReturnValueHandlers

@Nullable public org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite getReturnValueHandlers()
Return the configured handlers, or possibly `null` if not initialized yet via `afterPropertiesSet()`.

### setMessageConverters

public void setMessageConverters(List<org.springframework.http.converter.HttpMessageConverter<?>> messageConverters)
Set the message body converters to use.

These converters are used to convert from and to HTTP requests and responses.

### getMessageConverters

public List<org.springframework.http.converter.HttpMessageConverter<?>> getMessageConverters()
Return the configured message body converters.

### setContentNegotiationManager

public void setContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager contentNegotiationManager)
Set the `ContentNegotiationManager` to use to determine requested media types. If not set, the default constructor is used.

### getContentNegotiationManager

public org.springframework.web.accept.ContentNegotiationManager getContentNegotiationManager()
Return the configured `ContentNegotiationManager`.

### setResponseBodyAdvice

public void setResponseBodyAdvice(@Nullable List<ResponseBodyAdvice<?>> responseBodyAdvice)
Add one or more components to be invoked after the execution of a controller method annotated with `@ResponseBody` or returning `ResponseEntity` but before the body is written to the response with the selected `HttpMessageConverter`.

### setErrorResponseInterceptors

public void setErrorResponseInterceptors(List<org.springframework.web.ErrorResponse.Interceptor> interceptors)
Configure a list of `ErrorResponse.Interceptor`'s to apply when rendering an RFC 9457 `ProblemDetail` error response.

Parameters:
  `interceptors` - the handlers to use
Since:
  6.2

### getErrorResponseInterceptors

public List<org.springframework.web.ErrorResponse.Interceptor> getErrorResponseInterceptors()
Return the `configured` `ErrorResponse.Interceptor`'s.

Since:
  6.2

### setApplicationContext

public void setApplicationContext(@Nullable org.springframework.context.ApplicationContext applicationContext)

Specified by:
  `setApplicationContext` in interface `org.springframework.context.ApplicationContextAware`

### getApplicationContext

@Nullable public org.springframework.context.ApplicationContext getApplicationContext()

### afterPropertiesSet

public void afterPropertiesSet()

Specified by:
  `afterPropertiesSet` in interface `org.springframework.beans.factory.InitializingBean`

### getExceptionHandlerAdviceCache

public Map<org.springframework.web.method.ControllerAdviceBean,org.springframework.web.method.annotation.ExceptionHandlerMethodResolver> getExceptionHandlerAdviceCache()
Return an unmodifiable Map with the `@ControllerAdvice` beans discovered in the ApplicationContext. The returned map will be empty if the method is invoked before the bean has been initialized via `afterPropertiesSet()`.

### getDefaultArgumentResolvers

protected List<org.springframework.web.method.support.HandlerMethodArgumentResolver> getDefaultArgumentResolvers()
Return the list of argument resolvers to use including built-in resolvers and custom resolvers provided via `setCustomArgumentResolvers(java.util.List<org.springframework.web.method.support.HandlerMethodArgumentResolver>)`.

### getDefaultReturnValueHandlers

protected List<org.springframework.web.method.support.HandlerMethodReturnValueHandler> getDefaultReturnValueHandlers()
Return the list of return value handlers to use including built-in and custom handlers provided via `setReturnValueHandlers(java.util.List<org.springframework.web.method.support.HandlerMethodReturnValueHandler>)`.

### hasGlobalExceptionHandlers

protected boolean hasGlobalExceptionHandlers()
Description copied from class: `AbstractHandlerMethodExceptionResolver`
Whether this resolver has global exception handlers, for example, not declared in the same class as the `HandlerMethod` that raised the exception and therefore can apply to any handler.

Overrides:
  `hasGlobalExceptionHandlers` in class `AbstractHandlerMethodExceptionResolver`

### shouldApplyTo

protected boolean shouldApplyTo(HttpServletRequest request, @Nullable Object handler)
Description copied from class: `AbstractHandlerMethodExceptionResolver`
Checks if the handler is a `HandlerMethod` or the resolver has global exception handlers and then delegates to the base class implementation of `#shouldApplyTo` passing the bean of the `HandlerMethod` if necessary. Otherwise, returns `false`.

Overrides:
  `shouldApplyTo` in class `AbstractHandlerMethodExceptionResolver`
Parameters:
  `request` - current HTTP request
  `handler` - the executed handler, or `null` if none chosen at the time of the exception (for example, if multipart resolution failed)
Returns:
  whether this resolved should proceed with resolving the exception for the given request and handler
See Also:
  - `HandlerMethod`
  - `AbstractHandlerMethodExceptionResolver.hasGlobalExceptionHandlers()`

### doResolveHandlerMethodException

@Nullable protected ModelAndView doResolveHandlerMethodException(HttpServletRequest request, HttpServletResponse response, @Nullable org.springframework.web.method.HandlerMethod handlerMethod, Exception exception)
Find an `@ExceptionHandler` method and invoke it to handle the raised exception.

Specified by:
  `doResolveHandlerMethodException` in class `AbstractHandlerMethodExceptionResolver`
Parameters:
  `request` - current HTTP request
  `response` - current HTTP response
  `handlerMethod` - the executed handler method, or `null` if none chosen at the time of the exception (for example, if multipart resolution failed)
  `exception` - the exception that got thrown during handler execution
Returns:
  a corresponding ModelAndView to forward to, or `null` for default processing

### getExceptionHandlerMethod

@Nullable protected ServletInvocableHandlerMethod getExceptionHandlerMethod(@Nullable org.springframework.web.method.HandlerMethod handlerMethod, Exception exception, org.springframework.web.context.request.ServletWebRequest webRequest)
Find an `@ExceptionHandler` method for the given exception. The default implementation searches methods in the class hierarchy of the controller first and if not found, it continues searching for additional `@ExceptionHandler` methods assuming some @ControllerAdvice Spring-managed beans were detected.

Parameters:
  `handlerMethod` - the method where the exception was raised (may be `null`)
  `exception` - the raised exception
  `webRequest` - the original web request that resulted in a handler error
Returns:
  a method to handle the exception, or `null` if none