springBoot请求处理的大体流程
九大组件
- MultipartResolver 文件上传解析器
- LocaleResolver 国际信息解析器 国际化使用
- ThemeResolver 主题解析器 过时
- HandlerMapping 处理器映射 保存每一个映射和处理方法的关系
- HandlerAdapter 处理器适配器 反射调用handler的方法
- HandlerExceptionResolver 异常解析器
- RequestToViewNameTranslator
- FlashMapManager
- ViewResolver 视图解析器
请求处理过程
大体流程

尚硅谷简要版

doDispatch
protected void doDispatch(HttpServletRequest request, HttpServletResponse response) {
// Determine handler for the current request.
// 根据请求获取执行器链
mappedHandler = getHandler(processedRequest);
if (mappedHandler == null) {
noHandlerFound(processedRequest, response);
return;
}
// Determine handler adapter for the current request.
// 获取处理器适配器
// 使用了适配器模式
// 例如增加新的处理器类型时,该处理器只需要实现Adapter接口,此处的代码完全不需要修改
HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
// 执行拦截器的前置处理逻辑
if (!mappedHandler.applyPreHandle(processedRequest, response)) {
return;
}
// 实际执行controller中的方法
// Actually invoke the handler.
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
applyDefaultViewName(processedRequest, mv);
// 执行拦截器中的后置处理逻辑
mappedHandler.applyPostHandle(processedRequest, response, mv);
// 如果返回modelandView对象,使用对应的viewResolver进行渲染
processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);
}
//mappedHandler.applyPreHandle
boolean applyPreHandle(HttpServletRequest request, HttpServletResponse response) {
// 这里的this指代的就是上面获取到的执行器链
for (int i = 0; i < this.interceptorList.size(); i++) {
HandlerInterceptor interceptor = this.interceptorList.get(i);
if (!interceptor.preHandle(request, response, this.handler)) {
triggerAfterCompletion(request, response, null);
return false;
}
this.interceptorIndex = i;
}
return true;
}
//
private void processDispatchResult(HttpServletRequest request, HttpServletResponse response,
@Nullable HandlerExecutionChain mappedHandler, @Nullable ModelAndView mv,
@Nullable Exception exception) throws Exception {
boolean errorView = false;
// 如果存在异常
if (exception != null) {
if (exception instanceof ModelAndViewDefiningException mavDefiningException) {
logger.debug("ModelAndViewDefiningException encountered", exception);
mv = mavDefiningException.getModelAndView();
}
else {
Object handler = (mappedHandler != null ? mappedHandler.getHandler() : null);
mv = processHandlerException(request, response, handler, exception);
errorView = (mv != null);
}
}
// Did the handler return a view to render?
if (mv != null && !mv.wasCleared()) {
// 渲染
render(mv, request, response);
if (errorView) {
WebUtils.clearErrorRequestAttributes(request);
}
}
else {
if (logger.isTraceEnabled()) {
logger.trace("No view rendering, null ModelAndView returned.");
}
}
if (WebAsyncUtils.getAsyncManager(request).isConcurrentHandlingStarted()) {
// Concurrent handling started during a forward
return;
}
if (mappedHandler != null) {
// Exception (if any) is already handled..
mappedHandler.triggerAfterCompletion(request, response, null);
}
}
getHandler获取请求处理器
- 根据请求获取请求的执行链(controller以及对应的拦截器)
- 他这块的类结构有空好好看看,有点不太懂
protected HandlerExecutionChain getHandler(HttpServletRequest request){
if (this.handlerMappings != null) {
// 从一堆handleMapping中选择一个合适
for (HandlerMapping mapping : this.handlerMappings) {
HandlerExecutionChain handler = mapping.getHandler(request);
if (handler != null) {
return handler;
}
}
}
return null;
}
// mapping.getHandler(request); 方法
public final HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
// 获取处理器
Object handler = getHandlerInternal(request);
if (handler == null) {
handler = getDefaultHandler();
}
if (handler == null) {
return null;
}
// Bean name or resolved handler?
if (handler instanceof String handlerName) {
handler = obtainApplicationContext().getBean(handlerName);
}
// Ensure presence of cached lookupPath for interceptors and others
if (!ServletRequestPathUtils.hasCachedPath(request)) {
initLookupPath(request);
}
// 获取执行链,遍历所有拦截器,如果符合拦截条件将该拦截器添加到执行链中
HandlerExecutionChain executionChain = getHandlerExecutionChain(handler, request);
/*
...........其他逻辑代码
*/
return executionChain;
}
handlerMappings包含的类
-
RouterFunctionMapping 负责响应式编程的HTTP请求映射
-
RequestMappingHandlerMapping 负责MVC的HTTP请求映射
- 扫描@Controller,@RequestMaping相关注解
- 建立URL到请求处理方法的映射关系
-
WelcomePageHandlerMapping 处理欢迎页(默认首页)的特殊处理器映射组件。
-
BeanNameHandlerMapping 将 Bean 的名称直接作为 URL 路径来映射请求处理器(基本用不到)
-
WelcomePageNotAcceptableHandlerMapping 当客户端请求欢迎页(根路径
/)但无法接受服务器提供的响应格式时,这个处理器会返回 HTTP 406 Not Acceptable 状态码。 -
ResourceHandlerMapping 静态资源映射器

-
HttpMessageConverter 处理参数绑定转换器 例如将字符串转为对应的类,返回时将返回值转为对应的类型
-
ArgumentResolvers 解析参数
-
ReturnValueHandlers

浙公网安备 33010602011771号