001/* 002 * Copyright 2002-2017 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.web.servlet.handler; 018 019import javax.servlet.http.HttpServletRequest; 020import javax.servlet.http.HttpServletResponse; 021 022import org.springframework.lang.Nullable; 023import org.springframework.web.method.HandlerMethod; 024import org.springframework.web.servlet.ModelAndView; 025 026/** 027 * Abstract base class for 028 * {@link org.springframework.web.servlet.HandlerExceptionResolver HandlerExceptionResolver} 029 * implementations that support handling exceptions from handlers of type {@link HandlerMethod}. 030 * 031 * @author Rossen Stoyanchev 032 * @since 3.1 033 */ 034public abstract class AbstractHandlerMethodExceptionResolver extends AbstractHandlerExceptionResolver { 035 036 /** 037 * Checks if the handler is a {@link HandlerMethod} and then delegates to the 038 * base class implementation of {@code #shouldApplyTo(HttpServletRequest, Object)} 039 * passing the bean of the {@code HandlerMethod}. Otherwise returns {@code false}. 040 */ 041 @Override 042 protected boolean shouldApplyTo(HttpServletRequest request, @Nullable Object handler) { 043 if (handler == null) { 044 return super.shouldApplyTo(request, null); 045 } 046 else if (handler instanceof HandlerMethod) { 047 HandlerMethod handlerMethod = (HandlerMethod) handler; 048 handler = handlerMethod.getBean(); 049 return super.shouldApplyTo(request, handler); 050 } 051 else { 052 return false; 053 } 054 } 055 056 @Override 057 @Nullable 058 protected final ModelAndView doResolveException( 059 HttpServletRequest request, HttpServletResponse response, @Nullable Object handler, Exception ex) { 060 061 return doResolveHandlerMethodException(request, response, (HandlerMethod) handler, ex); 062 } 063 064 /** 065 * Actually resolve the given exception that got thrown during on handler execution, 066 * returning a ModelAndView that represents a specific error page if appropriate. 067 * <p>May be overridden in subclasses, in order to apply specific exception checks. 068 * Note that this template method will be invoked <i>after</i> checking whether this 069 * resolved applies ("mappedHandlers" etc), so an implementation may simply proceed 070 * with its actual exception handling. 071 * @param request current HTTP request 072 * @param response current HTTP response 073 * @param handlerMethod the executed handler method, or {@code null} if none chosen at the time 074 * of the exception (for example, if multipart resolution failed) 075 * @param ex the exception that got thrown during handler execution 076 * @return a corresponding ModelAndView to forward to, or {@code null} for default processing 077 */ 078 @Nullable 079 protected abstract ModelAndView doResolveHandlerMethodException( 080 HttpServletRequest request, HttpServletResponse response, @Nullable HandlerMethod handlerMethod, Exception ex); 081 082}