当前位置:网站首页>11. Custom Converter

11. Custom Converter

2022-08-10 23:48:00 If you want to learn, learn Big Wolf


11.1 基本介绍

  1. SpringBoot 在响应客户端请求时,Encapsulate the submitted data into objects时,使用了内置的转换器
  2. SpringBoot Custom converters are also supported,This built-in converter is in debug 的时候,可以看到
  3. 提供了 124 a built-in converter
  4. 看下源码 GenericConverter-ConvertiblePair

在这里插入图片描述

在这里插入图片描述

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

11.2.1 需求说明:Demonstrate custom converter usage

在这里插入图片描述


11.2.2 代码实现

  1. 修改 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>
  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 方法中,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;
                    }
                });
            }
        };
    }

}

  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 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);
            }
        };
    }

}

原网站

版权声明
本文为[If you want to learn, learn Big Wolf]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/222/202208102335002952.html