Class SimpleMappingExceptionResolver

java.lang.Object
org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver
org.springframework.web.servlet.handler.SimpleMappingExceptionResolver
All Implemented Interfaces:
Ordered, HandlerExceptionResolver

public class SimpleMappingExceptionResolver extends AbstractHandlerExceptionResolver
HandlerExceptionResolver implementation that allows for mapping exception class names to view names, either for a set of given handlers or for all handlers in the DispatcherServlet.

Error views are analogous to error page JSPs, but can be used with any kind of exception including any checked one, with fine-granular mappings for specific handlers.

Since:
22.11.2003
Author:
Juergen Hoeller, Arjen Poutsma, Rossen Stoyanchev
See Also:
  • Field Details

    • DEFAULT_EXCEPTION_ATTRIBUTE

      public static final String DEFAULT_EXCEPTION_ATTRIBUTE
      The default name of the exception attribute: "exception".
      See Also:
  • Constructor Details

    • SimpleMappingExceptionResolver

      public SimpleMappingExceptionResolver()
  • Method Details

    • setExceptionMappings

      public void setExceptionMappings(Properties mappings)
      Set the mappings between exception class names and error view names.

      The exception class name can be a substring, with no wildcard support at present. For example, a value of "ServletException" would match jakarta.servlet.ServletException and subclasses.

      NB: Consider carefully how specific the pattern is and whether to include package information (which isn't mandatory). For example, "Exception" will match nearly anything and will probably hide other rules. "java.lang.Exception" would be correct if "Exception" was meant to define a rule for all checked exceptions. With more unique exception names such as "BaseBusinessException" there's no need to use a fully-qualified class name.

      Parameters:
      mappings - exception patterns (can also be fully-qualified class names) as keys, and error view names as values
    • setExcludedExceptions

      public void setExcludedExceptions(Class<?>... excludedExceptions)
      Set one or more exceptions to be excluded from the exception mappings. Excluded exceptions are checked first and if one of them equals the actual exception, the exception will remain unresolved.
      Parameters:
      excludedExceptions - one or more excluded exception types
    • setDefaultErrorView

      public void setDefaultErrorView(String defaultErrorView)
      Set the name of the default error view. This view will be returned if no specific mapping was found.

      Default is none.

    • setStatusCodes

      public void setStatusCodes(Properties statusCodes)
      Set the HTTP status code that this exception resolver will apply for a given resolved error view. Keys are view names; values are status codes.

      Note that this error code will only get applied in case of a top-level request. It will not be set for an include request, since the HTTP status cannot be modified from within an include.

      If not specified, the default status code will be applied.

      See Also:
    • addStatusCode

      public void addStatusCode(String viewName, int statusCode)
      An alternative to setStatusCodes(Properties) for use with Java-based configuration.
    • getStatusCodesAsMap

      public Map<String,Integer> getStatusCodesAsMap()
      Returns the HTTP status codes provided via setStatusCodes(Properties). Keys are view names; values are status codes.
    • setDefaultStatusCode

      public void setDefaultStatusCode(int defaultStatusCode)
      Set the default HTTP status code that this exception resolver will apply if it resolves an error view and if there is no status code mapping defined.

      Note that this error code will only get applied in case of a top-level request. It will not be set for an include request, since the HTTP status cannot be modified from within an include.

      If not specified, no status code will be applied, either leaving this to the controller or view, or keeping the servlet engine's default of 200 (OK).

      Parameters:
      defaultStatusCode - the HTTP status code value, for example 500 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR) or 404 (HttpServletResponse.SC_NOT_FOUND)
      See Also:
    • setExceptionAttribute

      public void setExceptionAttribute(@Nullable String exceptionAttribute)
      Set the name of the model attribute as which the exception should be exposed. Default is "exception".

      This can be either set to a different attribute name or to null for not exposing an exception attribute at all.

      See Also:
    • doResolveException

      protected @Nullable ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, @Nullable Object handler, Exception ex)
      Actually resolve the given exception that got thrown during on 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 resolved 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
    • determineViewName

      protected @Nullable String determineViewName(Exception ex, HttpServletRequest request)
      Determine the view name for the given exception, first checking against the "excludedExecptions", then searching the "exceptionMappings", and finally using the "defaultErrorView" as a fallback.
      Parameters:
      ex - the exception that got thrown during handler execution
      request - current HTTP request (useful for obtaining metadata)
      Returns:
      the resolved view name, or null if excluded or none found
    • findMatchingViewName

      protected @Nullable String findMatchingViewName(Properties exceptionMappings, Exception ex)
      Find a matching view name in the given exception mappings.
      Parameters:
      exceptionMappings - mappings between exception class names and error view names
      ex - the exception that got thrown during handler execution
      Returns:
      the view name, or null if none found
      See Also:
    • getDepth

      protected int getDepth(String exceptionMapping, Exception ex)
      Return the depth to the superclass matching.

      0 means ex matches exactly. Returns -1 if there's no match. Otherwise, returns depth. Lowest depth wins.

    • determineStatusCode

      protected @Nullable Integer determineStatusCode(HttpServletRequest request, String viewName)
      Determine the HTTP status code to apply for the given error view.

      The default implementation returns the status code for the given view name (specified through the statusCodes property), or falls back to the defaultStatusCode if there is no match.

      Override this in a custom subclass to customize this behavior.

      Parameters:
      request - current HTTP request
      viewName - the name of the error view
      Returns:
      the HTTP status code to use, or null for the servlet container's default (200 in case of a standard error view)
      See Also:
    • applyStatusCodeIfPossible

      protected void applyStatusCodeIfPossible(HttpServletRequest request, HttpServletResponse response, int statusCode)
      Apply the specified HTTP status code to the given response, if possible (that is, if not executing within an include request).
      Parameters:
      request - current HTTP request
      response - current HTTP response
      statusCode - the status code to apply
      See Also:
    • getModelAndView

      protected ModelAndView getModelAndView(String viewName, Exception ex, HttpServletRequest request)
      Return a ModelAndView for the given request, view name and exception.

      The default implementation delegates to getModelAndView(String, Exception).

      Parameters:
      viewName - the name of the error view
      ex - the exception that got thrown during handler execution
      request - current HTTP request (useful for obtaining metadata)
      Returns:
      the ModelAndView instance
    • getModelAndView

      protected ModelAndView getModelAndView(String viewName, Exception ex)
      Return a ModelAndView for the given view name and exception.

      The default implementation adds the specified exception attribute. Can be overridden in subclasses.

      Parameters:
      viewName - the name of the error view
      ex - the exception that got thrown during handler execution
      Returns:
      the ModelAndView instance
      See Also: