在spring中,当一个请求过来的时候会做路径匹配,下面我们就从源码层面分析一下路径匹配。
示例:
@RequestMapping(value = "/user/{aid}/online/**", method = RequestMethod.GET)
我们一起看看这个方法是如何寻找的,和一些相应的工具类
1、入口
我的项目使用的是自动配置的RequestMappingHandlerMapping类,在getHandlerInternal()方法中:
HandlerMethod handlerMethod = lookupHandlerMethod(lookupPath, request);
上面这行是根据你的请求path和request去查找合适的method了。在项目启动的时候,Spring就把路径和对应的方法加载到了内存中。进入上面方法:
List<T> directPathMatches = this.mappingRegistry.getMappingsByUrl(lookupPath);
if (directPathMatches != null) {
addMatchingMappings(directPathMatches, matches, request);
}
if (matches.isEmpty()) {
// No choice but to go through all mappings...
addMatchingMappings(this.mappingRegistry.getMappings