基于SpringBoot的Web应用源码分析【中】
2021/9/5 14:06:26
本文主要是介绍基于SpringBoot的Web应用源码分析【中】,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
上篇:基于SpringBoot的Web应用源码分析【上】
一、Web应用源码分析
1、请求参数处理【rest使用与原理】
@RequestMapping:Rest风格支持(使用HTTP请求方式动词来表示对资源的操作)
以前:
/getUser | 获取用户 |
/deleteUser | 删除用户 |
/editUser | 修改用户 |
/saveUser | 保存用户 |
现在:
GET | 获取用户 |
DELETE | 删除用户 |
PUT | 修改用户 |
POST | 保存用户 |
核心Filter;HiddenHttpMethodFilter
用法: 表单method=post,隐藏域 _method=put
SpringBoot中手动开启
扩展:如何把_method 这个名字换成我们自己喜欢的。
1.1、案例
采用增删改查的请求方式提交表单
(1) 编写控制类
package org.apache.springboot.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController01 { @RequestMapping(value = "/user",method = RequestMethod.GET) public String getUser(){ return "GET-张三"; } @RequestMapping(value = "/user",method = RequestMethod.POST) public String saveUser(){ return "POST-张三"; } @RequestMapping(value = "/user",method = RequestMethod.PUT) public String putUser(){ return "PUT-张三"; } @RequestMapping(value = "/user",method = RequestMethod.DELETE) public String deleteUser(){ return "DELETE-张三"; } }
(2)编写静态页面【存放在resources文件目录下】
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>SpringBoot Web ,欢迎你</h1> 测试REST风格: <form action="/user" method="get"> <input value="REST-GET 提交" type="submit"/> </form> <form action="/user" method="post"> <input value="REST-POST 提交" type="submit"/> </form> <form action="/user" method="post"> <input name="_method" type="hidden" value="DELETE"/> <!-- 从源码分析得出,想要提交delete请求必须先用post请求去隐藏该参数,然后用delete去提交才行,若直接采用常规的post请求,delete请求是不生效的--> <input value="REST-DELETE 提交" type="submit"/> </form> <form action="/user" method="post"> <input name="_method" type="hidden" value="PUT"/> <!-- 从源码分析得出,想要提交delete请求必须先用post请求去隐藏该参数,然后用delete去提交才行,若直接采用常规的post请求,PUT请求是不生效的--> <input value="REST-PUT 提交" type="submit"/> </form> <hr/> 测试基本注解: <a href="/car/1">@PathVariable</a><br/> <a href="/car/1">@RequestHeader</a><br/> <a href="/cars?brand=byd">@RequestParam-GET</a><br/> <a href="/cars?brand=byd&brand=yd">@RequestParam-GET</a><br/> <a href="/cars?sell;low=34;brand=byd,audi,yd">@MatrixVariable(矩阵变量)</a> <a href="/cars?sell;low=34;brand=audi,brand=yd">@MatrixVariable(矩阵变量)</a> <a href="/boss/1;age=20/2;age=10">@MatrixVariable(矩阵变量)/boss/{bossId}/{empId}</a> <br/> <form action="save" method="post"> 测试@RequestBody获取数据<br/> 用户名:<input name="userName"/><br/> 邮箱:<input name="email"/> </form> </body> </html>
(3)配置页面表单的Rest功能【在application.yml配置】
如PostMan直接发送Put、delete等方式请求,无需Filte
spring: mvc: hiddenmethod: filter: enabled: true #开启页面表单的Rest功能
(4)启动启动类,并访问:http://localhost:8080/
1.2、Rest原理
表单提交要使用REST的时候,表单提交会带上_method=PUT,请求过来被HiddenHttpMethodFilter拦截
请求是否正常,并且是POST
- 获取到_method的值。
- 兼容以下请求;PUT.DELETE.PATCH
- 原生request(post),包装模式requesWrapper重写了getMethod方法,返回的是传入的值。
- 过滤器链放行的时候用wrapper。以后的方法调用getMethod是调用requesWrapper的。
1.3、请求映射原理
SpringMVC功能分析都从 org.springframework.web.servlet.DispatcherServlet--->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:处理器映射。/xxx->>xxxx
RequestMappingHandlerMapping:保存了所有@RequestMapping 和handler的映射规则。
所有的请求映射都在HandlerMapping中
(1)SpringBoot自动配置欢迎页的 WelcomePageHandlerMapping 。访问 /能访问到index.html;
(2)SpringBoot自动配置了默认 的 RequestMappingHandlerMapping
(3)请求进来,挨个尝试所有的HandlerMapping看是否有请求信息
- 如果有就找到这个请求对应的handler
- 如果没有就是下一个 HandlerMapping
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; }
这篇关于基于SpringBoot的Web应用源码分析【中】的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-01基于Python+Vue开发的医院门诊预约挂号系统
- 2024-10-01基于Python+Vue开发的旅游景区管理系统
- 2024-10-01RestfulAPI入门指南:打造简单易懂的API接口
- 2024-10-01初学者指南:了解和使用Server Action
- 2024-10-01Server Component入门指南:搭建与配置详解
- 2024-10-01React 中使用 useRequest 实现数据请求
- 2024-10-01使用 golang 将ETH账户的资产平均分散到其他账户
- 2024-10-01JWT用户校验课程:从入门到实践
- 2024-10-01Server Component课程入门指南
- 2024-09-30Dnd-Kit学习:新手快速入门指南