Class ExceptionHandlerMethodResolver

java.lang.Object
org.springframework.web.method.annotation.ExceptionHandlerMethodResolver

public class ExceptionHandlerMethodResolver extends Object
Discovers @ExceptionHandler methods in a given class, including all of its superclasses, and helps to resolve a given Exception and MediaType requested by clients to combinations supported by a given Method.

This resolver will use the exception information declared as @ExceptionHandler annotation attributes, or as a method argument as a fallback. This will throw IllegalStateException instances if:

  • No Exception information could be found for a method
  • An invalid MediaType has been declared as @ExceptionHandler attribute
  • Multiple handlers declare the same exception + media type mapping
Since:
3.1
Author:
Rossen Stoyanchev, Juergen Hoeller, Sam Brannen, Brian Clozel
  • Constructor Details

    • ExceptionHandlerMethodResolver

      public ExceptionHandlerMethodResolver(Class<?> handlerType)
      A constructor that finds ExceptionHandler methods in the given type.
      Parameters:
      handlerType - the type to introspect
      Throws:
      IllegalStateException - in case of invalid or ambiguous exception mapping declarations
  • Method Details

    • hasExceptionMappings

      public boolean hasExceptionMappings()
      Whether the contained type has any exception mappings.
    • resolveMethod

      @Nullable public Method resolveMethod(Exception exception)
      Find a Method to handle the given exception.

      Uses ExceptionDepthComparator if more than one match is found.

      Parameters:
      exception - the exception
      Returns:
      a Method to handle the exception, or null if none found
    • resolveMethodByThrowable

      @Nullable public Method resolveMethodByThrowable(Throwable exception)
      Find a Method to handle the given Throwable.

      Uses ExceptionDepthComparator if more than one match is found.

      Parameters:
      exception - the exception
      Returns:
      a Method to handle the exception, or null if none found
      Since:
      5.0
    • resolveExceptionMapping

      @Nullable public ExceptionHandlerMappingInfo resolveExceptionMapping(Throwable exception, MediaType mediaType)
      Find a Method to handle the given Throwable for the requested MediaType.

      Uses ExceptionDepthComparator and MediaType.isMoreSpecific(MimeType) if more than one match is found.

      Parameters:
      exception - the exception
      mediaType - the media type requested by the HTTP client
      Returns:
      a Method to handle the exception, or null if none found
      Since:
      6.2
    • resolveMethodByExceptionType

      @Nullable public Method resolveMethodByExceptionType(Class<? extends Throwable> exceptionType)
      Find a Method to handle the given exception type. This can be useful if an Exception instance is not available (e.g. for tools).

      Uses ExceptionDepthComparator if more than one match is found.

      Parameters:
      exceptionType - the exception type
      Returns:
      a Method to handle the exception, or null if none found
    • resolveExceptionMappingByExceptionType

      @Nullable public ExceptionHandlerMappingInfo resolveExceptionMappingByExceptionType(Class<? extends Throwable> exceptionType, MediaType mediaType)
      Find a Method to handle the given exception type and media type. This can be useful if an Exception instance is not available (e.g. for tools).
      Parameters:
      exceptionType - the exception type
      mediaType - the media type requested by the HTTP client
      Returns:
      a Method to handle the exception, or null if none found