请求映射源码

首先看一张请求完整流转图(这里感谢博客园上这位大神的图,博客地址我忘记了):

前台发送给后台的访问请求是如何找到对应的控制器映射并执行后续的后台操作呢,其核心为DispatcherServlet.java与HandlerMapper。在spring boot初始化的时候,将会加载所有的请求与对应的处理器映射为HandlerMapper组件。我们可以在springMVC的自动配置类中找到对应的Bean。

  1. @Bean
  2. @Primary
  3. @Override
  4. public RequestMappingHandlerMapping requestMappingHandlerMapping(
  5. @Qualifier("mvcContentNegotiationManager") ContentNegotiationManager contentNegotiationManager,
  6. @Qualifier("mvcConversionService") FormattingConversionService conversionService,
  7. @Qualifier("mvcResourceUrlProvider") ResourceUrlProvider resourceUrlProvider) {
  8. // Must be @Primary for MvcUriComponentsBuilder to work
  9. return super.requestMappingHandlerMapping(contentNegotiationManager, conversionService,
  10. resourceUrlProvider);
  11. }
  12. @Bean
  13. public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext,
  14. FormattingConversionService mvcConversionService, ResourceUrlProvider mvcResourceUrlProvider) {
  15. WelcomePageHandlerMapping welcomePageHandlerMapping = new WelcomePageHandlerMapping(
  16. new TemplateAvailabilityProviders(applicationContext), applicationContext, getWelcomePage(),
  17. this.mvcProperties.getStaticPathPattern());
  18. welcomePageHandlerMapping.setInterceptors(getInterceptors(mvcConversionService, mvcResourceUrlProvider));
  19. welcomePageHandlerMapping.setCorsConfigurations(getCorsConfigurations());
  20. return welcomePageHandlerMapping;
  21. }

请求将首先执行FrameworkServlet下的service方法根据request请求的method找到对应的do**方法。

  1. @Override
  2. protected void service(HttpServletRequest request, HttpServletResponse response)
  3. throws ServletException, IOException {
  4. HttpMethod httpMethod = HttpMethod.resolve(request.getMethod());
  5. if (httpMethod == HttpMethod.PATCH || httpMethod == null) {
  6. processRequest(request, response);
  7. }
  8. else {
  9. //父类根据method参数执行doGet,doPost,doDelete等
  10. super.service(request, response);
  11. }
  12. }

而这些do**其都会进入核心方法,以doGet为例。

  1. @Overrideprotected
  2. final void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  3. //核心方法
  4. processRequest(request, response);
  5. }
  1. protected final void processRequest(HttpServletRequest request, HttpServletResponse response)
  2. throws ServletException, IOException {
  3. try {
  4. //进入此核心方法
  5. doService(request, response);
  6. }
  7. catch (ServletException | IOException ex) {
  8. failureCause = ex;
  9. throw ex;
  10. }
  11. catch (Throwable ex) {
  12. failureCause = ex;
  13. throw new NestedServletException("Request processing failed", ex);
  14. }
  15. finally {
  16. resetContextHolders(request, previousLocaleContext, previousAttributes);
  17. if (requestAttributes != null) {
  18. requestAttributes.requestCompleted();
  19. }
  20. logResult(request, response, failureCause, asyncManager);
  21. publishRequestHandledEvent(request, response, startTime, failureCause);
  22. }

processRequest()方法中重点在doService(request, response);,而其核心处理逻辑位于DispatchServletl类重写的方法,如下。

  1. @Override
  2. protected void doService(HttpServletRequest request, HttpServletResponse response) throws Exception {
  3. ····
  4. try {
  5. //这里为实际分发控制器的逻辑,其内部是找到对应的handlerMapper
  6. doDispatch(request, response);
  7. }
  8. finally {
  9. if (!WebAsyncUtils.getAsyncManager(request).isConcurrentHandlingStarted()) {
  10. // Restore the original attribute snapshot, in case of an include.
  11. if (attributesSnapshot != null) {
  12. restoreAttributesAfterInclude(request, attributesSnapshot);
  13. }
  14. }
  15. if (requestPath != null) {
  16. ServletRequestPathUtils.clearParsedRequestPath(request);
  17. }
  18. }
  19. }

接下来看分发处理逻辑方法,其中重要的方法都使用了原生的注释。接下来分别分析核心源码。

  1. protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
  2. HttpServletRequest processedRequest = request;
  3. HandlerExecutionChain mappedHandler = null;
  4. boolean multipartRequestParsed = false;
  5. WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
  6. try {
  7. ModelAndView mv = null;
  8. Exception dispatchException = null;
  9. try {
  10. processedRequest = checkMultipart(request);
  11. multipartRequestParsed = (processedRequest != request);
  12. // Determine handler for the current request.
  13. mappedHandler = getHandler(processedRequest);
  14. if (mappedHandler == null) {
  15. noHandlerFound(processedRequest, response);
  16. return;
  17. }
  18. // Determine handler adapter for the current request.
  19. HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
  20. // Process last-modified header, if supported by the handler.
  21. String method = request.getMethod();
  22. boolean isGet = "GET".equals(method);
  23. if (isGet || "HEAD".equals(method)) {
  24. long lastModified = ha.getLastModified(request, mappedHandler.getHandler());
  25. if (new ServletWebRequest(request, response).checkNotModified(lastModified) && isGet) {
  26. return;
  27. }
  28. }
  29. if (!mappedHandler.applyPreHandle(processedRequest, response)) {
  30. return;
  31. }
  32. // Actually invoke the handler.
  33. mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
  34. if (asyncManager.isConcurrentHandlingStarted()) {
  35. return;
  36. }
  37. applyDefaultViewName(processedRequest, mv);
  38. mappedHandler.applyPostHandle(processedRequest, response, mv);
  39. }
  40. catch (Exception ex) {
  41. dispatchException = ex;
  42. }
  43. catch (Throwable err) {
  44. // As of 4.3, we're processing Errors thrown from handler methods as well,
  45. // making them available for @ExceptionHandler methods and other scenarios.
  46. dispatchException = new NestedServletException("Handler dispatch failed", err);
  47. }
  48. processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);
  49. }
  50. catch (Exception ex) {
  51. triggerAfterCompletion(processedRequest, response, mappedHandler, ex);
  52. }
  53. catch (Throwable err) {
  54. triggerAfterCompletion(processedRequest, response, mappedHandler,
  55. new NestedServletException("Handler processing failed", err));
  56. }
  57. finally {
  58. if (asyncManager.isConcurrentHandlingStarted()) {
  59. // Instead of postHandle and afterCompletion
  60. if (mappedHandler != null) {
  61. mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response);
  62. }
  63. }
  64. else {
  65. // Clean up any resources used by a multipart request.
  66. if (multipartRequestParsed) {
  67. cleanupMultipart(processedRequest);
  68. }
  69. }
  70. }
  71. }

首先是分析getHandler(),找到对应的处理器映射逻辑。

  1. protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
  2. if (this.handlerMappings != null) {
  3. for (HandlerMapping mapping : this.handlerMappings) {
  4. HandlerExecutionChain handler = mapping.getHandler(request);
  5. if (handler != null) {
  6. return handler;
  7. }
  8. }
  9. }
  10. return null;
  11. }

我们将断点标记在getHandler方法上时,可以清除看到handlerMappings,如图。

这里,用户请求与处理器的映射关系都在RequestMapperHandlerMapping中,而欢迎页处理请求则在WelcomePageHanderMapping中进行映射。

以下为RequestMapperHandlerMapping中映射部分截图,可以看到用户的所有请求映射这里面都有:

getHandler()后的方法是通过比较request请求中method与HandlerMapper中相同url下的method,再进行唯一性校验,不通过异常,通过找到唯一的handler。

后续,通过handler找到处理的设配器,通过适配器得到一个ModelAndView对象,这个对象就是最后返回给前端页面的对象。

至此,一个请求完整映射到返回前端结束。

说明:这是实现了FramworkServlet的doService方法,FramworkServlet继承自HttpServlet,并且重写了父类中的doGet(),doPost(),doPut(),doDelete 等方法,在这些重写的方法里都调用了 processRquest() 方法做请求处理,进入processRquest()可以看到里面调用了FramworkServlet中定义的doService() 方法。

SpringMVC请求映射handler源码解读的更多相关文章

  1. SpringMVC源码解读 - HandlerMapping

    SpringMVC在请求到handler处理器的分发这步是通过HandlerMapping模块解决的.handlerMapping 还处理拦截器. 先看看HandlerMapping的继承树吧 可以大 ...

  2. SpringMVC源码解读 - RequestMapping注解实现解读 - RequestCondition体系

    一般我们开发时,使用最多的还是@RequestMapping注解方式. @RequestMapping(value = "/", param = "role=guest& ...

  3. Flask(4)- flask请求上下文源码解读、http聊天室单聊/群聊(基于gevent-websocket)

    一.flask请求上下文源码解读 通过上篇源码分析,我们知道了有请求发来的时候就执行了app(Flask的实例化对象)的__call__方法,而__call__方法返回了app的wsgi_app(en ...

  4. flask的请求上下文源码解读

    一.flask请求上下文源码解读 通过上篇源码分析( ---Flask中的CBV和上下文管理--- ),我们知道了有请求发来的时候就执行了app(Flask的实例化对象)的__call__方法,而__ ...

  5. SpringMVC源码解读 - RequestMapping注解实现解读

    SpringMVC源码解读 - RequestMapping注解实现解读 - RequestCondition体系  https://www.cnblogs.com/leftthen/p/520840 ...

  6. Alamofire源码解读系列(十二)之请求(Request)

    本篇是Alamofire中的请求抽象层的讲解 前言 在Alamofire中,围绕着Request,设计了很多额外的特性,这也恰恰表明,Request是所有请求的基础部分和发起点.这无疑给我们一个Req ...

  7. SpringMVC源码解读 - RequestMapping注解实现解读 - RequestMappingInfo

    使用@RequestMapping注解时,配置的信息最后都设置到了RequestMappingInfo中. RequestMappingInfo封装了PatternsRequestCondition, ...

  8. CesiumJS 2022^ 源码解读[7] - 3DTiles 的请求、加载处理流程解析

    目录 1. 3DTiles 数据集的类型 2. 创建瓦片树 2.1. 请求入口文件 2.2. 创建树结构 2.3. 瓦片缓存机制带来的能力 3. 瓦片树的遍历更新 3.1. 三个大步骤 3.2. 遍历 ...

  9. Jfinal启动源码解读

    本文对Jfinal的启动源码做解释说明. PS:Jfinal启动容器可基于Tomcat/Jetty等web容器启动,本文基于Jetty的启动方式做启动源码的解读和分析,tomcat类似. 入口  JF ...

随机推荐

  1. 全方位构造免杀 webshell 小结[一]

    转载自https://klionsec.github.io/2017/10/11/bypasswaf-for-webshell/   全方位构造免杀 webshell 小结[一]   前言:    本 ...

  2. 用python写的一个自动卸载python包的脚本

    import osplist=os.popen("pip list") # 执行windows cmd命令,获取所有包package列表,并获取返回结果到plist#跳过第1,2行 ...

  3. macOS 需要更新软件才能连接到 iOS 设备

    macOS 需要更新软件才能连接到 iOS 设备 更新 Mac 上的软件 如果您在 iPhone.iPad 或 iPod touch 上看到"需要更新软件才能连接到 iOS 设备" ...

  4. WebIDE All In One

    WebIDE All In One web IDE Visual Studio Code vscode Code editing Redefined. Free. Built on open sour ...

  5. 使用 js 实现一个中文自动转换成拼音的工具库

    使用 js 实现一个中文自动转换成拼音的工具库 中文 => zhong-wen 应用场景 SEO 友好, URL 自动转换 blogs 发布文章,自动化部署,自动生成 url 的 path (时 ...

  6. Dart & data type(static / dynamic)

    Dart & data type(static / dynamic) Darts 飞镖 标枪 javelin/darts type https://dartpad.dartlang.org/ ...

  7. Angular Routing

    Angular Routing v9.0.7 https://angular.io/start/start-routing

  8. V8 & ECMAScript & ES-Next

    V8 & ECMAScript & ES-Next ES6, ES7, ES8, ES9, ES10, ES11, ES2015, ES2016, ES2017, ES2018, ES ...

  9. 基本ILS面的评估

    一.定义与用途 基本ILS面是ICAO DOC8168飞行程序设计规范中提到的一种限制面. 它相当于附件14中代码为3或4的精密进近跑道所规定的障碍物限制面的子集. 包含:进近面(分为两部分).过渡面 ...

  10. linux驱动系列之程序反汇编

    摘抄网页:http://www.169it.com/article/330129798173630299.html 参考网页:http://www.cppblog.com/liu1061/articl ...