当前位置:网站首页>八、请求处理之自定义类型参数绑定原理
八、请求处理之自定义类型参数绑定原理
2022-08-05 05:16:00 【呆比特】
请求处理之自定义类型参数绑定原理
这篇我们学习自定义参数绑定的原理,同样,先来做一个测试,有如下一个表单,表单对应下边两个类的属性,发送 saveuser 请求测试一下
/** * 姓名: <input name="userName"/> <br/> * 年龄: <input name="age"/> <br/> * 生日: <input name="birth"/> <br/> * 宠物姓名:<input name="pet.name"/><br/> * 宠物年龄:<input name="pet.age"/> */
@Data
public class Person {
private String userName;
private Integer age;
private Date birth;
private Pet pet;
}
@Data
public class Pet {
private String name;
private String age;
}
/** * 数据绑定:页面提交的请求数据(GET、POST)都可以和对象属性进行绑定 * @param person * @return */
@PostMapping("/saveuser")
public Person saveuser(Person person){
return person;
}

可以看到,我们提交的数据和对象自动就绑定包装好了
接下来,我们就是来探索它的原理是什么,走起…
照样前边分析过的不在赘述,直接看它获取的是哪个参数解析器
在 HandlerMethodArgumentResolverComposite.class 的 getArgumentResolver 方法,debug发现它拿到的是 ServletModelAttributeMethodProcessor ,也就是说,自定义类型的参数,我们用这个处理器


拿到了参数解析器,我们来看它如何处理
step into 来到 ModelAttributeMethodProcessor.class 的 resolveArgument 方法,前边一系列判断之后,调用了 createAttribute() 方法,这个方法为我们创建了一个空Person对象

然后就是进行绑定,利用 WebDataBinder 将请求中的所有数据,绑定到指定的JavaBean里边

WebDataBinder 利用它里面的 Converters 将请求数据转成指定的数据类型。然后封装到JavaBean中

在 bindRequestParameters 方法中,利用反射,获取我们请求里边的每一个参数值,然后找到我们JavaBean中的属性,将值封装到属性里边

进入bind()方法
进入doBind()方法
用我们的属性值
省略中间一些包装,到下边方法里边,就行转换器转换
用我们的转换器 Converter 转换好后,最后设置到JavaBean属性中,这就是们自定义类型参数绑定原理
如果你想要将传来的字符串,按照你的方式转换,也可以定制化 Converter , 方法就是给容器中放入自己的 WebMvcConfigurer ,重写里边的 addFormatters 方法,在里边添加自己的 Converter规则,快去试试吧!
边栏推荐
- MySQL
- [Kaggle project actual combat record] Steps and ideas sharing of a picture classification project - taking leaf classification as an example (using Pytorch)
- The difference between the operators and logical operators
- Tensorflow踩坑笔记,记录各种报错和解决方法
- 【数据库和SQL学习笔记】5.SELECT查询3:多表查询、连接查询
- CVPR2021 - Inception Convolution with Efficient Dilation Search
- OSPF故障排除办法
- 如何停止flink job
- 【数据库和SQL学习笔记】10.(T-SQL语言)函数、存储过程、触发器
- 神经网络也能像人类利用外围视觉一样观察图像
猜你喜欢

ECCV2022 | RU & Google propose zero-shot object detection with CLIP!

【论文阅读-表情捕捉】High-quality Real Time Facial Capture Based on Single Camera

CVPR best paper winner Huang Gao's team from Tsinghua University presented the first dynamic network review

网络信息安全运营方法论 (下)

【数据库和SQL学习笔记】3.数据操纵语言(DML)、SELECT查询初阶用法

大型Web网站高并发架构方案

网络ID,广播地址,掩码位数计算

It turns out that the MAE proposed by He Yuming is still a kind of data enhancement

网络信息安全运营方法论 (中)

flink yarn-session的两种使用方式
随机推荐
如何停止flink job
【ts】typescript高阶:条件类型与infer
【数据库和SQL学习笔记】9.(T-SQL语言)定义变量、高级查询、流程控制(条件、循环等)
面向小白的深度学习代码库,一行代码实现30+中attention机制。
网络信息安全运营方法论 (下)
Detailed explanation of BroadCast Receiver (broadcast)
关于使用QML的MediaPlayer实现视频和音频的播放时遇到的一些坑
【论文精读】ROC和PR曲线的关系(The relationship between Precision-Recall and ROC curves)
Facial Motion Capture 调研
[Database and SQL study notes] 10. (T-SQL language) functions, stored procedures, triggers
[Pytorch study notes] 11. Take a subset of the Dataset and shuffle the order of the Dataset (using Subset, random_split)
[Pytorch study notes] 10. How to quickly create your own Dataset dataset object (inherit the Dataset class and override the corresponding method)
【ts】typescript高阶:联合类型与交叉类型
Service
【Pytorch学习笔记】9.分类器的分类结果如何评估——使用混淆矩阵、F1-score、ROC曲线、PR曲线等(以Softmax二分类为例)
spingboot 容器项目完成CICD部署
基于STM32F407的WIFI通信(使用的是ESP8266模块)
It turns out that the MAE proposed by He Yuming is still a kind of data enhancement
单变量线性回归
【零基础开发NFT智能合约】如何使用工具自动生成NFT智能合约带白名单可Mint无需写代码