springboot系列--web相关知识探索二
一、映射
指的是与请求处理方法关联的URL路径,通过在Spring MVC的控制器类(使用@RestController注解修饰的类)上使用注解(如
@RequestMapping、@GetMapping)来指定请求映射路径,可以将不同的HTTP请求映射到相应的处理方法上。说白了就是将具体的请求映射到具体的接口当中。
二、springboot对Rest风格的支持
springboot默认情况下是支持Rest风格,但是唯独对表单类型的请求例外。表单情况下提交请求,无论你的method是put还是delete都会变成get请求。如果想要在表单类型下使用rest风格请求方式,需要把表单method属性设置为post,隐藏域设置为_method=put,这个时候请求才会进入到put类型的接口当中。
隐藏域:<input type="hidden" name="_method" value="delete">
原理:
表单提交以后,会被springboot中的OrderedHiddenHttpMethodFilter组件拦截,当然前提是开启了这个组件,不然表单也无法使用rest风格请求。OrderedHiddenHttpMethodFilter是继承HiddenHttpMethodFilter,请求会打到这个类的doFilterInternal方法上。该方法原理如下:
spring:
mvc:
hiddenmethod:
filter:
enabled: true #开启页面表单的Rest功能

protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {HttpServletRequest requestToUse = request;// 只有post请求才会打到这里,所以表单请求必须是postif ("POST".equals(request.getMethod()) && request.getAttribute("javax.servlet.error.exception") == null) {// 请求需要带this.methodParam对应的是_method,paramValue 对应的是具体的请求类型String paramValue = request.getParameter(this.methodParam);// 不为空的时候才进去处理if (StringUtils.hasLength(paramValue)) {String method = paramValue.toUpperCase(Locale.ENGLISH);// ALLOWED_METHODS 包含delete、put、patch等类型,也就是说隐藏域所带的值必须是这几个if (ALLOWED_METHODS.contains(method)) {// 这里就是对原始请求进行包装,将隐藏域的值作为新的请求类型。包装了原生请求的HttpMethodRequestWrapper类重写了getMethod方法,返回的是传入的methodrequestToUse = new HttpMethodRequestWrapper(request, method);}}}filterChain.doFilter((ServletRequest)requestToUse, response);}static {ALLOWED_METHODS = Collections.unmodifiableList(Arrays.asList(HttpMethod.PUT.name(), HttpMethod.DELETE.name(), HttpMethod.PATCH.name()));}
// 包装了原生请求的类
private static class HttpMethodRequestWrapper extends HttpServletRequestWrapper {private final String method;// 将外部的method重新赋值进来public HttpMethodRequestWrapper(HttpServletRequest request, String method) {super(request);this.method = method;}// 重写了getMethod,返回的是外部赋予的值public String getMethod() {return this.method;}}
表单提交指定了隐藏域需要带_method为key,值为put、patch、delete等参数,才能修改成符合rest风格请求。这是由于key="_method",是在底层源码写死了的。
public class HiddenHttpMethodFilter extends OncePerRequestFilter {private static final List<String> ALLOWED_METHODS;public static final String DEFAULT_METHOD_PARAM = "_method";// 此处写死了隐藏域key必须为_methodprivate String methodParam = "_method";public HiddenHttpMethodFilter() {}public void setMethodParam(String methodParam) {Assert.hasText(methodParam, "'methodParam' must not be empty");this.methodParam = methodParam;}protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {HttpServletRequest requestToUse = request;if ("POST".equals(request.getMethod()) && request.getAttribute("javax.servlet.error.exception") == null) {String paramValue = request.getParameter(this.methodParam);if (StringUtils.hasLength(paramValue)) {String method = paramValue.toUpperCase(Locale.ENGLISH);if (ALLOWED_METHODS.contains(method)) {requestToUse = new HttpMethodRequestWrapper(request, method);}}}filterChain.doFilter((ServletRequest)requestToUse, response);}static {ALLOWED_METHODS = Collections.unmodifiableList(Arrays.asList(HttpMethod.PUT.name(), HttpMethod.DELETE.name(), HttpMethod.PATCH.name()));}private static class HttpMethodRequestWrapper extends HttpServletRequestWrapper {private final String method;public HttpMethodRequestWrapper(HttpServletRequest request, String method) {super(request);this.method = method;}public String getMethod() {return this.method;}}
}
如果想要修改,可以重写这个组件。
//自定义filter@Beanpublic HiddenHttpMethodFilter hiddenHttpMethodFilter(){HiddenHttpMethodFilter methodFilter = new HiddenHttpMethodFilter();// 这个时候,key就必须等于_test了methodFilter.setMethodParam("_test");return methodFilter;}
三、请求映射原理
springmvc中所有的请求都是会先经过DispatcherServlet(前端控制器)这个类,这个类的其中一个父类HttpServlet,这个类有几个方法,doGet、doPost、doPut、doDelete,就是最原始处理get、put等请求的方法,HttpServlet是一个抽象类,这些方法是在FrameworkServlet中实现的。

里面每一个doGet、doPost等方法都调用了processRequest这个方法。

FrameworkServlet类中的这个方法是一个抽象方法,
protected abstract void doService(HttpServletRequest var1, HttpServletResponse var2) throws Exception;所以需要看它的子类DispatcherServlet里面的doService方法。这个方法调用了doDispatch这个方法,每个请求进来都是调用到它。
protected void doService(HttpServletRequest request, HttpServletResponse response) throws Exception {this.logRequest(request);Map<String, Object> attributesSnapshot = null;if (WebUtils.isIncludeRequest(request)) {attributesSnapshot = new HashMap();Enumeration<?> attrNames = request.getAttributeNames();label104:while(true) {String attrName;do {if (!attrNames.hasMoreElements()) {break label104;}attrName = (String)attrNames.nextElement();} while(!this.cleanupAfterInclude && !attrName.startsWith("org.springframework.web.servlet"));attributesSnapshot.put(attrName, request.getAttribute(attrName));}}request.setAttribute(WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.getWebApplicationContext());request.setAttribute(LOCALE_RESOLVER_ATTRIBUTE, this.localeResolver);request.setAttribute(THEME_RESOLVER_ATTRIBUTE, this.themeResolver);request.setAttribute(THEME_SOURCE_ATTRIBUTE, this.getThemeSource());if (this.flashMapManager != null) {FlashMap inputFlashMap = this.flashMapManager.retrieveAndUpdate(request, response);if (inputFlashMap != null) {request.setAttribute(INPUT_FLASH_MAP_ATTRIBUTE, Collections.unmodifiableMap(inputFlashMap));}request.setAttribute(OUTPUT_FLASH_MAP_ATTRIBUTE, new FlashMap());request.setAttribute(FLASH_MAP_MANAGER_ATTRIBUTE, this.flashMapManager);}RequestPath previousRequestPath = null;if (this.parseRequestPath) {previousRequestPath = (RequestPath)request.getAttribute(ServletRequestPathUtils.PATH_ATTRIBUTE);ServletRequestPathUtils.parseAndCache(request);}try {// 这行代码之前,其实大部分逻辑都是初始化设置各种值,doDispatch就是做各种转发this.doDispatch(request, response);} finally {if (!WebAsyncUtils.getAsyncManager(request).isConcurrentHandlingStarted() && attributesSnapshot != null) {this.restoreAttributesAfterInclude(request, attributesSnapshot);}ServletRequestPathUtils.setParsedRequestPath(previousRequestPath, request);}}
下面是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);// 找到当前请求使用哪个Handler(Controller的方法)处理mappedHandler = getHandler(processedRequest);//HandlerMapping:处理器映射。
@Nullableprotected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {// 循环遍历每个处理器映射器if (this.handlerMappings != null) {Iterator var2 = this.handlerMappings.iterator();while(var2.hasNext()) {// 处理请求的处理器映射器RequestMappingHandlerMappingHandlerMapping mapping = (HandlerMapping)var2.next();// 通过url获取到具体的处理器映射器HandlerExecutionChain handler = mapping.getHandler(request);if (handler != null) {return handler;}}}return null;}
1、this.handlerMappings != null中handlerMappings就是springboot所有的处理器映射器。2、其中RequestMappingHandlerMapping:保存了所有@RequestMapping 和handler的映射规则。其实就是保存了@RequestMapping中配置的各种url和对应的controller
3、 所有的请求映射都在HandlerMapping中。然后通过HandlerMapping找到请求url对应的具体的controller。
4、SpringBoot自动配置欢迎页的 WelcomePageHandlerMapping 。访问 /能访问到index.html;


springboot底层通过url获取到具体的controller原理,需要从
HandlerExecutionChain handler = mapping.getHandler(request);
这个方法进入

protected HandlerMethod getHandlerInternal(HttpServletRequest request) throws Exception {// 获取@GetMapping("/test1")上的请求路径/test1String lookupPath = this.initLookupPath(request);//mappingRegistry保存了所有的请求路径对应的controller信息,加锁是防止并发获取this.mappingRegistry.acquireReadLock();HandlerMethod var4;try {// 根据url和请求信息,找到controller中对应能够处理的方法,其实就是具体的那个接口的信息HandlerMethod handlerMethod = this.lookupHandlerMethod(lookupPath, request);var4 = handlerMethod != null ? handlerMethod.createWithResolvedBean() : null;} finally {this.mappingRegistry.releaseReadLock();}return var4;}// 这个方法主要是获取url请求路径protected String initLookupPath(HttpServletRequest request) {if (this.usesPathPatterns()) {request.removeAttribute(UrlPathHelper.PATH_ATTRIBUTE);RequestPath requestPath = ServletRequestPathUtils.getParsedRequestPath(request);String lookupPath = requestPath.pathWithinApplication().value();return UrlPathHelper.defaultInstance.removeSemicolonContent(lookupPath);} else {// 请求进来是在这里处理的return this.getUrlPathHelper().resolveAndCacheLookupPath(request);}}/**String lookupPath:注解上的请求路径HttpServletRequest request:具体请求**/@Nullableprotected HandlerMethod lookupHandlerMethod(String lookupPath, HttpServletRequest request) throws Exception {List<AbstractHandlerMethodMapping<T>.Match> matches = new ArrayList();// 通过url找到多个个能够处理的接口或者说方法。比如说/test1可能会有put请求,也有get请求。post请求等等List<T> directPathMatches = this.mappingRegistry.getMappingsByDirectPath(lookupPath);if (directPathMatches != null) {// 找到了多个接口,就是添加到这个方法里,这个方法会根据你请求进来的方式(post、get或put等等)匹配到最优的处理方法,也就是精确找到你这个进来的请求方式所对应的处理接口。并且把匹配到的放到第一位且一般只有一个,具体不展开叙述this.addMatchingMappings(directPathMatches, matches, request);}if (matches.isEmpty()) {this.addMatchingMappings(this.mappingRegistry.getRegistrations().keySet(), matches, request);}if (matches.isEmpty()) {return this.handleNoMatch(this.mappingRegistry.getRegistrations().keySet(), lookupPath, request);} else {// 这里就是从上面保存符合条件的集合中获取第一个,一般只有一个,如果有两个,例如/test1 get请求,写了两个方法且请求方式,路径都一样,就matches.size() > 1就为true,然后会进入里面的处理逻辑并报错AbstractHandlerMethodMapping<T>.Match bestMatch = (Match)matches.get(0);// 就是这里if (matches.size() > 1) {Comparator<AbstractHandlerMethodMapping<T>.Match> comparator = new MatchComparator(this.getMappingComparator(request));matches.sort(comparator);bestMatch = (Match)matches.get(0);if (this.logger.isTraceEnabled()) {this.logger.trace(matches.size() + " matching mappings: " + matches);}if (CorsUtils.isPreFlightRequest(request)) {Iterator var7 = matches.iterator();while(var7.hasNext()) {AbstractHandlerMethodMapping<T>.Match match = (Match)var7.next();if (match.hasCorsConfig()) {return PREFLIGHT_AMBIGUOUS_MATCH;}}} else {AbstractHandlerMethodMapping<T>.Match secondBestMatch = (Match)matches.get(1);if (comparator.compare(bestMatch, secondBestMatch) == 0) {Method m1 = bestMatch.getHandlerMethod().getMethod();Method m2 = secondBestMatch.getHandlerMethod().getMethod();String uri = request.getRequestURI();// 有两个相同的url且请求方式一致的处理方法throw new IllegalStateException("Ambiguous handler methods mapped for '" + uri + "': {" + m1 + ", " + m2 + "}");}}}request.setAttribute(BEST_MATCHING_HANDLER_ATTRIBUTE, bestMatch.getHandlerMethod());this.handleMatch(bestMatch.mapping, lookupPath, request);// 这里就是获取到具体的处理方法,也就是接口对应的方法,例如/test1 对应的是com.example.estest.controller.MvcTestController#test()这个接口,会返回com.example.estest.controller.MvcTestController#test()这个东西的具体信息return bestMatch.getHandlerMethod();}}


1、另外SpringBoot自动配置了默认配置了一个处理器映射器就是 RequestMappingHandlerMapping用于处理@GetMapping、@PusMapping、@RequestMapping等注解的映射
2、如果需要一些自定义的映射处理,我们也可以自己给容器中放HandlerMapping。自定义 HandlerMapping。可以参考WebMvcAutoConfiguration类下的requestMappingHandlerMapping组件定义。
@Bean@Primarypublic RequestMappingHandlerMapping requestMappingHandlerMapping(@Qualifier("mvcContentNegotiationManager") ContentNegotiationManager contentNegotiationManager, @Qualifier("mvcConversionService") FormattingConversionService conversionService, @Qualifier("mvcResourceUrlProvider") ResourceUrlProvider resourceUrlProvider) {return super.requestMappingHandlerMapping(contentNegotiationManager, conversionService, resourceUrlProvider);}
相关文章:
springboot系列--web相关知识探索二
一、映射 指的是与请求处理方法关联的URL路径,通过在Spring MVC的控制器类(使用RestController注解修饰的类)上使用注解(如 RequestMapping、GetMapping)来指定请求映射路径,可以将不同的HTTP请求映射到相应…...
Oracle 12c在Windows环境下安装
适合初学者使用的Oracle 12c在Windows环境下安装步骤、参数配置、常见问题及参数调优的详细补充说明。 一、Oracle 12c安装步骤 1. 准备工作 在安装Oracle 12c之前,确保你的系统满足以下要求: 操作系统:Oracle 12c支持的Windows版本包括Wi…...
Stable Diffusion绘画 | 来训练属于自己的模型:打标处理与优化
上一篇完成的打标工作,是为了获取提示词,让AI认识和学习图片的特征。 因此,合适、恰当、无误的提示词,对最终模型效果是相当重要的。 Tag 如何优化 通过软件自动生成的 Tag 只是起到快速建立大体架构的作用,里面会涉…...
【论文笔记】Visual Instruction Tuning
🍎个人主页:小嗷犬的个人主页 🍊个人网站:小嗷犬的技术小站 🥭个人信条:为天地立心,为生民立命,为往圣继绝学,为万世开太平。 基本信息 标题: Visual Instruction Tunin…...
ubuntu 设置静态IP
一、 ip addresssudo nano /etc/netplan/50-cloud-init.yaml 修改前: 修改后: # This file is generated from information provided by the datasource. Changes # to it will not persist across an instance reboot. To disable cloud-inits # ne…...
Java 每日一刊(第19期):泛型
文章目录 前言1. 泛型概述1.1 不使用泛型 vs 使用泛型1.2 泛型的作用 2. 泛型的基本语法2.1 定义带类型参数的泛型类2.2 使用泛型类2.3 泛型方法 3. 泛型类型推断与钻石操作符3.1 类型推断3.2 钻石操作符 4. 通配符的使用4.1 无界通配符 <?>4.2 上界通配符 <? exten…...
windows下安装rabbitMQ并开通管理界面和允许远程访问
如题,在windows下安装一个rabbitMQ server;然后用浏览器访问其管理界面;由于rabbitMQ的默认账号guest默认只能本机访问,因此需要设置允许其他机器远程访问。这跟mysql的思路很像,默认只能本地访问,要远程访…...
深度剖析音频剪辑免费工具的特色与优势
是热爱生活的伙伴或者想要记录美好声音的普通用户,都可能会需要对音频进行剪辑处理。而幸运的是,现在有许多优秀的音频剪辑软件提供了免费版本,让我们能够轻松地施展音频剪辑的魔法。接下来,就让我们一同深入了解这些音频剪辑免费…...
Oracle中TRUNC()函数详解
文章目录 前言一、TRUNC函数的语法二、主要用途三、测试用例总结 前言 在Oracle中,TRUNC函数用于截取或截断日期、时间或数值表达式的部分。它返回一个日期、时间或数值的截断版本,根据提供的格式进行截取。 一、TRUNC函数的语法 TRUNC(date) TRUNC(d…...
【Spring Boot 入门一】构建你的第一个Spring Boot应用
一、引言 在当今的软件开发领域,Java一直占据着重要的地位。而Spring Boot作为Spring框架的延伸,为Java开发者提供了一种更加便捷、高效的开发方式。它简化了Spring应用的搭建和配置过程,让开发者能够专注于业务逻辑的实现。无论是构建小型的…...
PPT 快捷键使用、技巧
前言: 本文操作是以office 2021为基础的,仅供参考;不同版本office 的 ppt 快捷键 以及对应功能会有差异,需要实践出真知。 shift 移动 水平/垂直 移动 ; shift 放大/缩小 等比例放大 缩小 ; 正圆 正…...
Web安全 - 文件上传漏洞(File Upload Vulnerability)
文章目录 OWASP 2023 TOP 10导图定义攻击场景1. 上传恶意脚本2. 目录遍历3. 覆盖现有文件4. 文件上传结合社会工程攻击 防御措施1. 文件类型验证2. 文件名限制3. 文件存储位置4. 文件权限设置5. 文件内容检测6. 访问控制7. 服务器配置 文件类型验证实现Hutool的FileTypeUtil使用…...
vue3中el-input在form表单按下回车刷新页面
摘要: 在input框中点击回车之后不是调用我写的回车事件,而是刷新页面! 如果表单中只有一个input 框则按下回车会直接关闭表单 所以导致刷新页面 再写一个input 表单 ,并设置style“display:none” <ElInput style"display…...
SQL Server中关于个性化需求批量删除表的做法
在实际开发中,我们常常会遇到需要批量删除表,且具有共同特征的情况,例如:找出表名中数字结尾的表之类的,本文我将以3中类似情况为例,来示范并解说此类需求如何完成: 第一种,批量删除…...
关于按键状态机解决Delay给程序带来的问题
问题产生 我在学习中断的过程中,使用EXTI15外部中断,在其中加入HAL_Delay();就会发生报错 错误地方 其它地方配置 问题原因 在中断服务例程(ISR)中使用 HAL_Delay() 会导致问题的原因是: 阻塞性: HAL_D…...
62.【C语言】浮点数的存储
目录 1.浮点数的类型 2.浮点数表示的范围 3.浮点数的特性 《计算机科学导论》的叙述 4.浮点数在内存中的存储 答案速查 分析 前置知识:浮点数的存储规则 推导单精度浮点数5.5在内存中的存储 验证 浮点数取出的分析 1.一般情况:E不全为0或不全为1 2.特殊情况:E全为0…...
GO网络编程(一):基础知识
1. 网络编程的基础概念 TCP/IP 协议栈 TCP/IP 是互联网通信的核心协议栈,分为以下四个层次: 应用层(Application Layer):为应用程序提供网络服务的协议,比如 HTTP、FTP、SMTP 等。传输层(Tra…...
【Linux】用虚拟机配置Ubuntu环境
目录 1.虚拟机安装Ubuntu系统 2.Ubuntu系统的网络配置 3.特别声明 首先我们先要下载VMware软件,大家自己去下啊! 1.虚拟机安装Ubuntu系统 我们进去之后点击创建新的虚拟机,然后选择自定义 接着点下一步 再点下一步 进入这个界面之后&…...
酒店智能门锁SDK接口pro[V10] 门锁校验C#-SAAS本地化-未来之窗行业应用跨平台架构
一、代码 int 酒店标识_int Convert.ToInt32(酒店标识);StringBuilder 锁号2024 new StringBuilder(8);//信息 "未知返回值:" bufCard_原始;GetGuestLockNoByCardDataStr_原始(酒店标识_int, bufCard_原始.ToString(), 锁号2024);StringBuilder 退…...
Gitのrebase用法
在 Git 中,rebase 是一种用于整合多个提交历史的操作,它可以将一个分支的变更“重放”到另一个分支上。与 merge 不同,rebase 会产生一个线性的提交历史,使得项目的历史记录更加整洁和易于理解。 1. 什么是 Rebase? …...
【Go】3、Go语言进阶与依赖管理
前言 本系列文章参考自稀土掘金上的 【字节内部课】公开课,做自我学习总结整理。 Go语言并发编程 Go语言原生支持并发编程,它的核心机制是 Goroutine 协程、Channel 通道,并基于CSP(Communicating Sequential Processes࿰…...
【python异步多线程】异步多线程爬虫代码示例
claude生成的python多线程、异步代码示例,模拟20个网页的爬取,每个网页假设要0.5-2秒完成。 代码 Python多线程爬虫教程 核心概念 多线程:允许程序同时执行多个任务,提高IO密集型任务(如网络请求)的效率…...
全面解析各类VPN技术:GRE、IPsec、L2TP、SSL与MPLS VPN对比
目录 引言 VPN技术概述 GRE VPN 3.1 GRE封装结构 3.2 GRE的应用场景 GRE over IPsec 4.1 GRE over IPsec封装结构 4.2 为什么使用GRE over IPsec? IPsec VPN 5.1 IPsec传输模式(Transport Mode) 5.2 IPsec隧道模式(Tunne…...
精益数据分析(97/126):邮件营销与用户参与度的关键指标优化指南
精益数据分析(97/126):邮件营销与用户参与度的关键指标优化指南 在数字化营销时代,邮件列表效度、用户参与度和网站性能等指标往往决定着创业公司的增长成败。今天,我们将深入解析邮件打开率、网站可用性、页面参与时…...
Python基于历史模拟方法实现投资组合风险管理的VaR与ES模型项目实战
说明:这是一个机器学习实战项目(附带数据代码文档),如需数据代码文档可以直接到文章最后关注获取。 1.项目背景 在金融市场日益复杂和波动加剧的背景下,风险管理成为金融机构和个人投资者关注的核心议题之一。VaR&…...
Python Ovito统计金刚石结构数量
大家好,我是小马老师。 本文介绍python ovito方法统计金刚石结构的方法。 Ovito Identify diamond structure命令可以识别和统计金刚石结构,但是无法直接输出结构的变化情况。 本文使用python调用ovito包的方法,可以持续统计各步的金刚石结构,具体代码如下: from ovito…...
【从零学习JVM|第三篇】类的生命周期(高频面试题)
前言: 在Java编程中,类的生命周期是指类从被加载到内存中开始,到被卸载出内存为止的整个过程。了解类的生命周期对于理解Java程序的运行机制以及性能优化非常重要。本文会深入探寻类的生命周期,让读者对此有深刻印象。 目录 …...
push [特殊字符] present
push 🆚 present 前言present和dismiss特点代码演示 push和pop特点代码演示 前言 在 iOS 开发中,push 和 present 是两种不同的视图控制器切换方式,它们有着显著的区别。 present和dismiss 特点 在当前控制器上方新建视图层级需要手动调用…...
【从零开始学习JVM | 第四篇】类加载器和双亲委派机制(高频面试题)
前言: 双亲委派机制对于面试这块来说非常重要,在实际开发中也是经常遇见需要打破双亲委派的需求,今天我们一起来探索一下什么是双亲委派机制,在此之前我们先介绍一下类的加载器。 目录 编辑 前言: 类加载器 1. …...
学习一下用鸿蒙DevEco Studio HarmonyOS5实现百度地图
在鸿蒙(HarmonyOS5)中集成百度地图,可以通过以下步骤和技术方案实现。结合鸿蒙的分布式能力和百度地图的API,可以构建跨设备的定位、导航和地图展示功能。 1. 鸿蒙环境准备 开发工具:下载安装 De…...
