请求映射原理

DispatcherServlet>AbstractHandlerMapping>AbstractHandlerMethodMapping。
在这里插入图片描述

1. DispatcherServlet#doDispatch

doDispatch,确定 当前请求 应该由 哪个handler(或controller或处理器)的方法 处理。

protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
	//...
	mappedHandler = this.getHandler(processedRequest);
	//...
}
2. DispatcherServlet#getHandler
protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
    if (this.handlerMappings != null) {
        Iterator var2 = this.handlerMappings.iterator();

        while(var2.hasNext()) {
            HandlerMapping mapping = (HandlerMapping)var2.next();
            HandlerExecutionChain handler = mapping.getHandler(request);
            if (handler != null) {
                return handler;
            }
        }
    }

    return null;
}

在这里插入图片描述
可以看到处理器映射器有以下5种:

  • RequestMappingHandlerMapping
  • WelcomePageHandlerMapping
  • BeanNameUrlHandlerMapping
  • RouterFunctionMapping
  • SimpleUrlHandlerMapping
3. AbstractHandlerMapping#getHandler
public final HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
    //...
    Object handler = this.getHandlerInternal(request);
    //...
}
4. AbstractHandlerMethodMapping#getHandlerInternal
protected HandlerMethod getHandlerInternal(HttpServletRequest request) throws Exception {
    String lookupPath = this.initLookupPath(request);
    this.mappingRegistry.acquireReadLock();

    HandlerMethod var4;
    try {
        HandlerMethod handlerMethod = this.lookupHandlerMethod(lookupPath, request);
        var4 = handlerMethod != null ? handlerMethod.createWithResolvedBean() : null;
    } finally {
        this.mappingRegistry.releaseReadLock();
    }

    return var4;
}

在这里插入图片描述

5. AbstractHandlerMethodMapping#lookupHandlerMethod
protected HandlerMethod lookupHandlerMethod(String lookupPath, HttpServletRequest request) throws Exception {
    List<AbstractHandlerMethodMapping<T>.Match> matches = new ArrayList();
    List<T> directPathMatches = this.mappingRegistry.getMappingsByDirectPath(lookupPath);
    //...
    if (directPathMatches != null) {
        this.addMatchingMappings(directPathMatches, matches, request);
    }
    if (matches.isEmpty()) {
        return this.handleNoMatch(this.mappingRegistry.getRegistrations().keySet(), lookupPath, request);
    } else {
        AbstractHandlerMethodMapping<T>.Match bestMatch = (AbstractHandlerMethodMapping.Match)matches.get(0);
		//...
        request.setAttribute(BEST_MATCHING_HANDLER_ATTRIBUTE, bestMatch.getHandlerMethod());
        this.handleMatch(bestMatch.mapping, lookupPath, request);
        return bestMatch.getHandlerMethod();
    }
}

在这里插入图片描述
在这里插入图片描述
最终找到 GET /user这个请求路径由com.example.boot.controller.Demo3Controller#get()处理,即由控制器Demo3Controller的get方法处理。

下面演示调试过程。

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值