欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

123. 【深入解析SpringBoot源码B】(第四部分)

最编程 2024-08-14 21:57:35
...
(1.2)、修改默认的_method更改为自定义的参数

假如在业务的实际开发中,我们不想使用SpringBoot提供的默认的参数 _methods 作为我们的rest请求映射,我们可以对这个组件进行重写赋值即可。

  1. 查看源码
public class HiddenHttpMethodFilter extends OncePerRequestFilter {
    private static final List<String> ALLOWED_METHODS;
    public static final String DEFAULT_METHOD_PARAM = "_method";
 ⭐   private String methodParam = "_method";
    public HiddenHttpMethodFilter() {
    }
    public void setMethodParam(String methodParam) {
        Assert.hasText(methodParam, "'methodParam' must not be empty");
        this.methodParam = methodParam;
    }
}
  1. 开始重写这个源码组件
package com.jsxs.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.HiddenHttpMethodFilter;
/**
 * @Author Jsxs
 * @Date 2023/7/3 11:13
 * @PackageName:com.jsxs.config
 * @ClassName: WebConfig
 * @Description: TODO
 * @Version 1.0
 */
@Configuration(proxyBeanMethods = false)
public class WebConfig {
    @Bean
    public HiddenHttpMethodFilter hiddenHttpMethodFilter(){
        HiddenHttpMethodFilter hiddenHttpMethodFilter = new HiddenHttpMethodFilter();
        hiddenHttpMethodFilter.setMethodParam("aaaa");
        return hiddenHttpMethodFilter;
    }
}

这里我们对其进行修改了 name的值为 aaaa

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>你好</h1>
<form action="/user" method="post">
    <button type="submit">Post方式进行跳转</button>
</form>
<form action="/user" method="get">
    <button type="submit">Get方式进行跳转</button>
</form>
<form action="/user" method="post">
    ⭐⭐⭐
    <input name="aaaa" type="hidden" value="DELETE">
    <button type="submit">DELETE方式进行跳转</button>
</form>
<form action="/user" method="post">
    ⭐⭐⭐
    <input name="aaaa" type="hidden" value="PUT">
    <button type="submit">PUT方式进行跳转</button>
</form>
</body>
</html>
  1. 测试运行

网络异常,图片无法展示
|

网络异常,图片无法展示
|

(1.3)、请求映射原理

CTRL+H 打开的是 继承树

CTRL+F12 打开的是 方法结构

网络异常,图片无法展示
|

SpringMVC功能分析都从 org.springframework.web.servlet.DispatcherServlet-》doDispatch()

最重要的是 doDispatch()

网络异常,图片无法展示
|

protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
  ⭐ HttpServletRequest processedRequest = request;
    HandlerExecutionChain mappedHandler = null;
    boolean multipartRequestParsed = false;
    WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
    try {
      ModelAndView mv = null;
      Exception dispatchException = null;
      try {
        processedRequest = checkMultipart(request);
        multipartRequestParsed = (processedRequest != request);
        // 找到当前的我们处理器要跳转的路径 (/xxx)
    ⭐⭐    mappedHandler = getHandler(processedRequest);
                
                //HandlerMapping:处理器映射。/xxx->>xxxx
  1. 第一个断点我们可以得到我们要请求的路径和方式

2.第二个断点

我们步入进 mappedHandler = getHandler(processedRequest);

@Nullable
  protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
  ⭐ if (this.handlerMappings != null) {
      for (HandlerMapping mapping : this.handlerMappings) {
  ⭐⭐      HandlerExecutionChain handler = mapping.getHandler(request);
        if (handler != null) {
          return handler;
        }
      }
    }
    return null;
  }

RequestMappingHandlerMapping:保存了所有@RequestMapping 和handler的映射规则。

所有的请求映射都在HandlerMapping中。

  • SpringBoot自动配置欢迎页的 WelcomePageHandlerMapping 。访问 /能访问到index.html;
  • SpringBoot自动配置了默认 的 RequestMappingHandlerMapping
  • 请求进来,挨个尝试所有的HandlerMapping 链表看是否有请求信息。
  • 如果有就找到这个请求对应的handler
  • 如果没有就是下一个 HandlerMapping
  • 我们需要一些自定义的映射处理,我们也可以自己给容器中放HandlerMapping。自定义 HandlerMapping