当前位置:网站首页>11. 自定义转换器
11. 自定义转换器
2022-08-10 23:35:00 【要学就学灰太狼】
11.1 基本介绍
- SpringBoot 在响应客户端请求时,将提交的数据封装成对象时,使用了内置的转换器
- SpringBoot 也支持自定义转换器,这个内置转换器在 debug 的时候,可以看到
- 提供了 124 个内置转换器
- 看下源码 GenericConverter-ConvertiblePair
11.2 自定义转换器-应用实例
11.2.1 需求说明:演示自定义转换器使用
11.2.2 代码实现
- 修改 save.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>添加小妖怪</title>
</head>
<body>
<h1>添加妖怪-坐骑[测试封装 POJO]</h1>
<form action="/saveMonster" method="post">
编号: <input name="id" value="100"><br/>
姓名: <input name="name" value="牛魔王"/> <br/>
年龄: <input name="age" value="120"/> <br/>
婚否: <input name="isMarried" value="true"/> <br/>
生日: <input name="birth" value="2000/11/11"/> <br/>
<!--坐骑: <input name="car.name" value="法拉利"/><br/>-->
<!--价格: <input name="car.price" value="99999.9"/>-->
<!--使用自定义转换器关联Car, 字符串整体提交 使用 ,号 间隔-->
坐骑: <input name="car" value="避水金睛兽,888.8"/><br/>
<input type="submit" value="保存"/>
</form>
</body>
</html>
- 创建自定义转换器 D:\xjs_springboot\springbootweb\src\main\java\com\xjs\springboot\config\WebConfig.java
package com.xjs.springboot.config;
import com.xjs.springboot.bean.Car;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.format.FormatterRegistry;
import org.springframework.util.ObjectUtils;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/** * @Author: 谢家升 * @Version: 1.0 */
/** * 解读:@Configuration(proxyBeanMethods = false) * 1. WebConfig 是一个配置类 * 2. proxyBeanMethods = false 表示使用了 lite模式 */
@Configuration(proxyBeanMethods = false)
public class WebConfig {
//注入一个Bean WebMvcConfigurer
@Bean
public WebMvcConfigurer webMvcConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addFormatters(FormatterRegistry registry) {
/** * 解读: * 1. 在 addFormatters 方法中,增加一个自定义的转换器 * 2. 增加自定义转换器 String -> Car * 3. 增加的自定义转换器会注册到 converters 容器中 * 4. converters 底层结构是 ConcurrentHashMap 内置有124个转换器[不同版本个数可能不同] * 5. 一会儿 Debug */
registry.addConverter(new Converter<String, Car>() {
@Override
public Car convert(String source) {
//source 就是传入的字符串 => 避水金睛兽,888.8
//这里就加入你的自定义的转换业务代码
if (!ObjectUtils.isEmpty(source)) {
Car car = new Car();
String[] split = source.split(",");
car.setName(split[0]);
car.setPrice(Double.parseDouble(split[1]));
return car;
}
return null;
}
});
}
};
}
}
- 完成测试,浏览器 http://localhost:8080/save.html
monster-Monster(id=100, name=牛魔王, age=120, isMarried=true, birth=Sat Nov 11 00:00:00 CST 2000, car=Car(name=避水金睛兽, price=888.8))
- Debug 可以看到我们新增的 Converter
11.2.3 注册转换器换种写法-方便理解
package com.xjs.springboot.config;
import com.xjs.springboot.bean.Car;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.format.FormatterRegistry;
import org.springframework.util.ObjectUtils;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/** * @Author: 谢家升 * @Version: 1.0 */
/** * 解读:@Configuration(proxyBeanMethods = false) * 1. WebConfig 是一个配置类 * 2. proxyBeanMethods = false 表示使用了 lite模式 */
@Configuration(proxyBeanMethods = false)
public class WebConfig {
//注入一个Bean WebMvcConfigurer
@Bean
public WebMvcConfigurer webMvcConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addFormatters(FormatterRegistry registry) {
/** * 解读: * 1. 在 addFormatters 方法中,增加一个自定义的转换器 * 2. 增加自定义转换器 String -> Car * 3. 增加的自定义转换器会注册到 converters 容器中 * 4. converters 底层结构是 ConcurrentHashMap 内置有124个转换器[不同版本个数可能不同] * 5. 一会儿 Debug */
//registry.addConverter(new Converter<String, Car>() {
// @Override
// public Car convert(String source) { //source 就是传入的字符串 => 避水金睛兽,888.8
// //这里就加入你的自定义的转换业务代码
// if (!ObjectUtils.isEmpty(source)) {
//
// Car car = new Car();
// String[] split = source.split(",");
// car.setName(split[0]);
// car.setPrice(Double.parseDouble(split[1]));
// return car;
//
// }
// return null;
// }
//});
//换一种写法来注册自定义转换器-方便理解
//1. 先创建自定义的转换器
Converter<String, Car> xjsConverter = new Converter<String, Car>() {
@Override
public Car convert(String source) {
//source 就是传入的字符串 => 避水金睛兽,888.8
//这里就加入你的自定义的转换业务代码
if (!ObjectUtils.isEmpty(source)) {
Car car = new Car();
String[] split = source.split(",");
car.setName(split[0]);
car.setPrice(Double.parseDouble(split[1]));
return car;
}
return null;
}
};
//还可以增加更多的转换器
//2. 添加转化器到 converters
registry.addConverter(xjsConverter);
}
};
}
}
----- 细节说明 -----
- 添加转化器到 converters
key
=>[源类型->目标类型]
- converters 底层是 ConcurrentHashMap
- 其中key是 [源类型->目标类型]
- 所以当两个转换器的 源类型 和 目标类型 相同,则会覆盖
- 这里添加的 第3个自定义转换器 会覆盖 第1个自定义转换器 [都是 String -> Car]
package com.xjs.springboot.config;
import com.xjs.springboot.bean.Car;
import com.xjs.springboot.bean.Monster;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.format.FormatterRegistry;
import org.springframework.util.ObjectUtils;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/** * @Author: 谢家升 * @Version: 1.0 */
/** * 解读:@Configuration(proxyBeanMethods = false) * 1. WebConfig 是一个配置类 * 2. proxyBeanMethods = false 表示使用了 lite模式 */
@Configuration(proxyBeanMethods = false)
public class WebConfig {
//注入一个Bean WebMvcConfigurer
@Bean
public WebMvcConfigurer webMvcConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addFormatters(FormatterRegistry registry) {
/** * 解读: * 1. 在 addFormatters 方法中,增加一个自定义的转换器 * 2. 增加自定义转换器 String -> Car * 3. 增加的自定义转换器会注册到 converters 容器中 * 4. converters 底层结构是 ConcurrentHashMap 内置有124个转换器[不同版本个数可能不同] * 5. 一会儿 Debug */
//registry.addConverter(new Converter<String, Car>() {
// @Override
// public Car convert(String source) { //source 就是传入的字符串 => 避水金睛兽,888.8
// //这里就加入你的自定义的转换业务代码
// if (!ObjectUtils.isEmpty(source)) {
//
// Car car = new Car();
// String[] split = source.split(",");
// car.setName(split[0]);
// car.setPrice(Double.parseDouble(split[1]));
// return car;
//
// }
// return null;
// }
//});
//换一种写法来注册自定义转换器-方便理解
//1. 先创建自定义的转换器
Converter<String, Car> xjsConverter = new Converter<String, Car>() {
@Override
public Car convert(String source) {
//source 就是传入的字符串 => 避水金睛兽,888.8
//这里就加入你的自定义的转换业务代码
if (!ObjectUtils.isEmpty(source)) {
Car car = new Car();
String[] split = source.split(",");
car.setName(split[0]);
car.setPrice(Double.parseDouble(split[1]));
return car;
}
return null;
}
};
//还可以增加更多的转换器
//第2个自定义转换器
Converter<String, Monster> xjsConverter2 = new Converter<String, Monster>() {
@Override
public Monster convert(String source) {
//source 就是传入的字符串 => 避水金睛兽,888.8
//这里就加入你的自定义的转换业务代码
return null;
}
};
//第3个自定义转换器
Converter<String, Car> xjsConverter3 = new Converter<String, Car>() {
@Override
public Car convert(String source) {
//source 就是传入的字符串 => 避水金睛兽,888.8
System.out.println("source= " + source);
return null;
}
};
//2. 添加转化器到 converters key-[源类型->目标类型]
// (1) converters 底层是 ConcurrentHashMap
// (2) 其中key是 [源类型->目标类型]
// (3) 所以当两个转换器 的源类型和 目标类型 相同,则会覆盖
// (4) 这里添加的 第3个自定义转换器 会覆盖 第1个自定义转换器
registry.addConverter(xjsConverter);
registry.addConverter(xjsConverter2);
registry.addConverter(xjsConverter3);
}
};
}
}
边栏推荐
猜你喜欢
随机推荐
2022牛客多校(七)K. Great Party博弈方法证明
关于弱监督学习的详细介绍——A Brief Introduction to Weakly Supervised Learning
CW617N锡青铜 CuZn40Pb2对应牌号
推进牛仔服装的高质量发展
How to determine how many bases a number is?
App的回归测试,有什么高效的测试方法?
缓存知识总结
逮到一个阿里 10 年老 测试开发,聊过之后收益良多...
宝塔实测-搭建PHP在线模拟考试系统
小程序制作开发应遵循哪些原则?
XSLeaks side channel attack (unfinished)
How to recover deleted files from the recycle bin, two methods of recovering files from the recycle bin
国内vr虚拟全景技术领先的公司
使用PageHelper自定义PageInfo进行分页+模糊查询
vr虚拟仿真教学系统应用场景开发
一条SQL查询语句是如何执行的?
3d虚拟现实数字化展厅给予参观者充分的交互性和空间感
基于SSM实现手机销售商城系统
Kotlin 由浅入深,带你搞懂协程异常处理的那点事
ROS实验笔记之——UZH-FPV数据集的验证