11. 自定义转换器


11.1 基本介绍

  1. SpringBoot 在响应客户端请求时,将提交的数据封装成对象时,使用了内置的转换器
  2. SpringBoot 也支持自定义转换器,这个内置转换器在 debug 的时候,可以看到
  3. 提供了 124 个内置转换器
  4. 看下源码 GenericConverter-ConvertiblePair

在这里插入图片描述

在这里插入图片描述

11.2 自定义转换器-应用实例

11.2.1 需求说明:演示自定义转换器使用

在这里插入图片描述


11.2.2 代码实现

  1. 修改 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>
  1. 创建自定义转换器 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;
                    }
                });
            }
        };
    }

}

  1. 完成测试,浏览器 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))
  1. 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);
            }
        };
    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

要学就学灰太狼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值