当前位置:网站首页>11. Custom Converter
11. Custom Converter
2022-08-10 23:48:00 【If you want to learn, learn Big Wolf】
文章目录
11.1 基本介绍
- SpringBoot 在响应客户端请求时,Encapsulate the submitted data into objects时,使用了内置的转换器
- SpringBoot Custom converters are also supported,This built-in converter is in debug 的时候,可以看到
- 提供了 124 a built-in converter
- 看下源码 GenericConverter-ConvertiblePair


11.2 自定义转换器-应用实例
11.2.1 需求说明:Demonstrate custom converter usage

11.2.2 代码实现
- 修改 save.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Add little monsters</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"/>-->
<!--Use custom converter associationsCar, The string as a whole is submitted 使用 ,号 间隔-->
坐骑: <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 方法中,Add a custom converter * 2. Add custom converters String -> Car * 3. Added custom converters will be registered to converters 容器中 * 4. converters 底层结构是 ConcurrentHashMap 内置有124个转换器[The number of different versions may vary] * 5. 一会儿 Debug */
registry.addConverter(new Converter<String, Car>() {
@Override
public Car convert(String source) {
//source 就是传入的字符串 => 避水金睛兽,888.8
//Add your custom conversion business code here
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 Register the converter in a different way-方便理解
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 方法中,Add a custom converter * 2. Add custom converters String -> Car * 3. Added custom converters will be registered to converters 容器中 * 4. converters 底层结构是 ConcurrentHashMap 内置有124个转换器[The number of different versions may vary] * 5. 一会儿 Debug */
//registry.addConverter(new Converter<String, Car>() {
// @Override
// public Car convert(String source) { //source 就是传入的字符串 => 避水金睛兽,888.8
// //Add your custom conversion business code here
// 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;
// }
//});
//Write another way to register custom converters-方便理解
//1. Create a custom converter first
Converter<String, Car> xjsConverter = new Converter<String, Car>() {
@Override
public Car convert(String source) {
//source 就是传入的字符串 => 避水金睛兽,888.8
//Add your custom conversion business code here
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;
}
};
//More converters can also be added
//2. Add converter to converters
registry.addConverter(xjsConverter);
}
};
}
}
----- 细节说明 -----
- Add converter to converters
key=>[源类型->目标类型]- converters 底层是 ConcurrentHashMap
- 其中key是 [源类型->目标类型]
- So when two converters are 源类型 和 目标类型 相同,则会覆盖
- 这里添加的 第3a custom converter 会覆盖 第1a custom converter [都是 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 方法中,Add a custom converter * 2. Add custom converters String -> Car * 3. Added custom converters will be registered to converters 容器中 * 4. converters 底层结构是 ConcurrentHashMap 内置有124个转换器[The number of different versions may vary] * 5. 一会儿 Debug */
//registry.addConverter(new Converter<String, Car>() {
// @Override
// public Car convert(String source) { //source 就是传入的字符串 => 避水金睛兽,888.8
// //Add your custom conversion business code here
// 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;
// }
//});
//Write another way to register custom converters-方便理解
//1. Create a custom converter first
Converter<String, Car> xjsConverter = new Converter<String, Car>() {
@Override
public Car convert(String source) {
//source 就是传入的字符串 => 避水金睛兽,888.8
//Add your custom conversion business code here
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;
}
};
//More converters can also be added
//第2a custom converter
Converter<String, Monster> xjsConverter2 = new Converter<String, Monster>() {
@Override
public Monster convert(String source) {
//source 就是传入的字符串 => 避水金睛兽,888.8
//Add your custom conversion business code here
return null;
}
};
//第3a custom converter
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. Add converter to converters key-[源类型->目标类型]
// (1) converters 底层是 ConcurrentHashMap
// (2) 其中key是 [源类型->目标类型]
// (3) So when two converters the source type and 目标类型 相同,则会覆盖
// (4) 这里添加的 第3a custom converter 会覆盖 第1a custom converter
registry.addConverter(xjsConverter);
registry.addConverter(xjsConverter2);
registry.addConverter(xjsConverter3);
}
};
}
}
边栏推荐
猜你喜欢

【C语言】初识指针
![[C] the C language program design, dynamic address book (order)](/img/bb/b44066e91219a57b653e330198e4a0.png)
[C] the C language program design, dynamic address book (order)
![[C language articles] Expression evaluation (implicit type conversion, arithmetic conversion)](/img/ae/74dc5fc676e74ab03607565ca7539e.png)
[C language articles] Expression evaluation (implicit type conversion, arithmetic conversion)

u盘数据不小心删除怎么恢复,u盘数据删除如何恢复

服务器小常识

小程序制作开发应遵循哪些原则?

8. WEB 开发-静态资源访问

【C语言篇】操作符之 位运算符详解(“ << ”,“ >> ”,“ & ”,“ | ”,“ ^ ”,“ ~ ”)

Metasploit——客户端渗透

10. 接收参数相关注解
随机推荐
Rust从入门到精通05-语句和表达式
进程和线程
【C语言】初识指针
如果纯做业务测试的话,在测试行业有出路吗?
企业小程序怎么开发自己的小程序?
Kubernetes 选举机制HA
[C language articles] Expression evaluation (implicit type conversion, arithmetic conversion)
Android面试冲刺:2022全新面试题——剑指Offer(备战金九银十)
C语言篇,操作符之 移位运算符(>>、<<)详解
CSDN21天学习挑战赛之折半插入排序
[C Language Chapter] Detailed explanation of bitwise operators (“<<”, “>>”, “&”, “|”, “^”, “~”)
[C language] First understanding of pointers
6.0深入理解MySQL事务隔离级别与锁机制
[C language] Implementation of guessing number game
Activiti7子流程之Call activity
[C] the C language program design, dynamic address book (order)
11. 自定义转换器
HGAME 2022 Week3 writeup
2022牛客多校(七)K. Great Party博弈方法证明
22年全国程序员1月薪资出炉,才知道年薪 40 万以上的有这么多?