本文开始逐步揭开我们最常用的 @RequestMapping 注解的神秘面纱。
-
我们将一起了解被 @RequestMapping 注解的方法是如何“注册”到 DispatcherServlet 中的?
-
在 http 请求到达服务器之后,又是如何找到“合适”的被 @RequestMapping 注解的方法?
-
http 请求的参数是如何变为 @RequestMapping 方法中的参数的?
-
@RequestMapping 方法的返回值又是如何变成我们期望的网页或者内容的?
DispatcherServlet 分发时序图
image.png从左向右看,当我们向 SpringMVC 服务器发起一个请求时,会进入到 DisptacherServlet # doDispatch 中。
- 获取处理器:调用 DisptacherServlet # getHandler,根据 HttpServletRequest 推断出我们需要的“处理器对象”,最终返回 HandlerExecutionChain 对象。
public class HandlerExecutionChain {
// 处理器对象
private final Object handler;
/**
* Return the handler object to execute.
* 正因为是 Object 对象,才需要适配器来对应
*/
public Object getHandler() {
return this.handler;
}
}
-
获取适配器:接着调用 DisptacherServlet # getHandlerAdapter,为处理器对象 HandlerExecutionChain # handler 选择出一个支持处理该对象的适配器。
-
执行适配器/处理器:本文中选出的适配器是 RequestMappingHandlerAdapter,现在就调用该适配器的 handle 方法
MappingRegistry 是 AbstractHandlerMethodMapping<T> 的一个内部类,用来存放 url 和 HandlerMethod 的对应关系,RequestMappingInfo 和 HandlerMethod 的对应关系等,方便根据 url 来获取对应的方法。
在此列一个stacktrack 来 具体描述调用的顺序
StackTrace (spring-webmvc-5.3.24)
org.springframework.web.servlet.mvc.condition.PatternsRequestCondition.getMatchingCondition(PatternsRequestCondition.java:280)
org.springframework.web.servlet.mvc.method.RequestMappingInfo.getMatchingCondition(RequestMappingInfo.java:406)
org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.getMatchingMapping(RequestMappingInfoHandlerMapping.java:109)
org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.getMatchingMapping(RequestMappingInfoHandlerMapping.java:67)
org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.addMatchingMappings(AbstractHandlerMethodMapping.java:448)
org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.lookupHandlerMethod(AbstractHandlerMethodMapping.java:408)
org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:383)
org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.getHandlerInternal(RequestMappingInfoHandlerMapping.java:125)
org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.getHandlerInternal(RequestMappingInfoHandlerMapping.java:67)
org.springframework.web.servlet.handler.AbstractHandlerMapping.getHandler(AbstractHandlerMapping.java:498)
org.springframework.web.servlet.DispatcherServlet.getHandler(DispatcherServlet.java:1265)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1047)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:964)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
javax.servlet.http.HttpServlet.service(HttpServlet.java:670)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
javax.servlet.http.HttpServlet.service(HttpServlet.java:779)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:711)
org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:459)
org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:385)
org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:313)
javax.servlet.http.HttpServlet.service(HttpServlet.java:779)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:711)
org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:459)
org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:353)
org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:313)
com.ebay.raptor.kernel.filter.RaptorDispatchFilter.doFilter(RaptorDispatchFilter.java:63)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
com.ebay.app.raptor.resbarn.provider.ResourceFilter.doFilter(ResourceFilter.java:32)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:177)
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:360)
org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399)
org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:891)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1784)
org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:748)
总结
小结一下,我们现在可以知道,想要解决开篇的问题,要去哪里找源码和寻求答案了!
如果我们想知道 HttpServletRequest 和 HandlerMethod 的对应关系的 扫描注册 和 匹配获取,我们可以去 RequestMappingHandlerMapping 中去寻找。
如果我们想知道 参数处理 和 结果集处理 的逻辑,我们可以去 RequestMappingHandlerAdapter 中去寻找。
网友评论