SpringBoot 全局异常处理用法及原理
Springboot或springMVC项目中, 我们一般会设置一个全局异常处理, 来对异常进行兜底。 业务代码执行过程中抛出的异常, 如果业务逻辑没有主动捕获,那么异常就会一直往上抛,最后进入全局异常处理逻辑。
本文和大家探讨SpringBoot 全局异常处理用法及原理, 整体分为三个部分:第一部分,讲spring如何处理一个http请求异常;第二部分, 讲全局异常处理的几种方法; 第三部分,探究一下全局异常处理的底层原理。
只关心用法的同学,可以直接跳到第二部分。
一、springboot如何处理一个http请求异常
SpingBoot中,web请求由DispatcherServlet类的doDispatch方法来处理,如果处理过程抛出了异常,processDispatchResult方法会对异常进行处理。 此处省略了一些无关的代码。
protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpServletRequest processedRequest = request;
HandlerExecutionChain mappedHandler = null;
boolean multipartRequestParsed = false;
WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
try {
ModelAndView mv = null;
Exception dispatchException = null;
try {
processedRequest = checkMultipart(request);
multipartRequestParsed = (processedRequest != request);
// Determine handler for the current request.
mappedHandler = getHandler(processedRequest);
if (mappedHandler == null || mappedHandler.getHandler() == null) {
noHandlerFound(processedRequest, response);
return;
}
// Determine handler adapter for the current request.
HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
// code omitted
// Actually invoke the handler.
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
if (asyncManager.isConcurrentHandlingStarted()) {
return;
}
applyDefaultViewName(processedRequest, mv);
mappedHandler.applyPostHandle(processedRequest, response, mv);
}
catch (Exception ex) {
dispatchException = ex;
}
catch (Throwable err) {
// As of 4.3, we're processing Errors thrown from handler methods as well,
// making them available for @ExceptionHandler methods and other scenarios.
dispatchException = new NestedServletException("Handler dispatch failed", err);
}
// this is where thrown exception is handled 处理异常的地方
processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);
}
// code omitted
}
}
processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException)方法是异常处理的入口,然后委派给processHandlerException(request, response, handler, exception)方法来处理。
/**
* Handle the result of handler selection and handler invocation, which is
* either a ModelAndView or an Exception to be resolved to a ModelAndView.
*
*/
private void processDispatchResult(HttpServletRequest request, HttpServletResponse response,
HandlerExecutionChain mappedHandler, ModelAndView mv, Exception exception) throws Exception {
boolean errorView = false;
if (exception != null) {
if (exception instanceof ModelAndViewDefiningException) {
logger.debug("ModelAndViewDefiningException encountered", exception);
mv = ((ModelAndViewDefiningException) exception).getModelAndView();
}
else {
// get handler from handlerchain, in most cases the hanlder is a HanlderMethod
Object handler = (mappedHandler != null ? mappedHandler.getHandler() : null);
// delegate to this method 委派给processHandlerException
mv = processHandlerException(request, response, handler, exception);
errorView = (mv != null);
}
}
// code omitted
protected ModelAndView processHandlerException(HttpServletRequest request, HttpServletResponse response,
Object handler, Exception ex) throws Exception {
// Check registered HandlerExceptionResolvers...
ModelAndView exMv = null;
// 重点代码
for (HandlerExceptionResolver handlerExceptionResolver : this.handlerExceptionResolvers) {
exMv = handlerExceptionResolver.resolveException(request, response, handler, ex);
if (exMv != null) {
break;
}
}
if (exMv != null)