• 【commons-beanutils专题】005- ConvertUtils 专题


    【commons-beanutils专题】005- ConvertUtils 专题

    一、准备

    0、ConvertUtils 主要作用

    主要用于类型转换,可自定义转换器!

    1、引入 commons-beanutils 依赖

    
    <dependency>
        <groupId>commons-beanutilsgroupId>
        <artifactId>commons-beanutilsartifactId>
        <version>1.9.4version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2、pom.xml 文件

    
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0modelVersion>
        <parent>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-parentartifactId>
            <version>2.7.1version>
            <relativePath/> 
        parent>
        <groupId>com.zibogroupId>
        <artifactId>zibo2022artifactId>
        <version>0.0.1-SNAPSHOTversion>
        <name>zibo2022name>
        <description>zibo2022description>
        <properties>
            <java.version>17java.version>
        properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
    
            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
                <optional>trueoptional>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-testartifactId>
                <scope>testscope>
            dependency>
    
            
            <dependency>
                <groupId>commons-beanutilsgroupId>
                <artifactId>commons-beanutilsartifactId>
                <version>1.9.4version>
            dependency>
        dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-maven-pluginartifactId>
                    <version>2.7.1version>
                    <configuration>
                        <excludes>
                            <exclude>
                                <groupId>org.projectlombokgroupId>
                                <artifactId>lombokartifactId>
                            exclude>
                        excludes>
                    configuration>
                plugin>
            plugins>
        build>
    
    project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62

    3、实体类

    Cat

    package com.zibo.zibo2022.convert_utils.entity;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class Cat {
    
        private String name;
    
        private Integer age;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    CatDto

    package com.zibo.zibo2022.convert_utils.entity;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    /**
     * @author zibo
     * @date 2022/7/20 0020 18:56
     */
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class CatDto {
    
        private String name;
    
        private Integer age;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    4、自定义转换器

    package com.zibo.zibo2022.convert_utils.converter;
    
    import com.zibo.zibo2022.convert_utils.entity.Cat;
    import com.zibo.zibo2022.convert_utils.entity.CatDto;
    import java.lang.reflect.InvocationTargetException;
    import org.apache.commons.beanutils.Converter;
    import org.apache.commons.beanutils.PropertyUtils;
    
    /**
     * 此处简单写一个转换器,用于将 Cat 对象转换为 CatDto 对象
     *
     * @author zibo
     * @date 2022/7/20 0020 19:00
     */
    public class CatConverter implements Converter {
        @Override
        public <T> T convert(Class<T> type, Object value) {
            try {
                if (!(value instanceof Cat)) {
                    throw new IllegalArgumentException("参数类型错误!");
                }
                if (type != CatDto.class) {
                    throw new IllegalArgumentException("参数类型错误!");
                }
                String name = (String) PropertyUtils.getProperty(value, "name");
                Integer age = (Integer) PropertyUtils.getProperty(value, "age");
                T newInstance = type.getDeclaredConstructor().newInstance();
                PropertyUtils.setProperty(newInstance, "name", name);
                PropertyUtils.setProperty(newInstance, "age", age);
                return newInstance;
            } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException | InstantiationException e) {
                throw new RuntimeException(e);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    5、前置代码

    Cat cat = new Cat("小猫", 1);
    Cat cat2 = new Cat("小猫2", 2);
    
    • 1
    • 2

    二、类型转换

    1、将对象转换为字符串

    // 1、将对象转换为字符串
    String convert = ConvertUtils.convert(cat);
    System.out.println(convert); // Cat(name=小猫, age=1)
    // 下面的内容是值得注意的,如果是数组,只转换第一个元素
    String convert20 = ConvertUtils.convert(new Cat[] {cat, cat2});
    System.out.println(convert20); // Cat(name=小猫, age=1)
    String convert40 = ConvertUtils.convert(new String[] {"小猫", "1", "随便写的内容"});
    System.out.println(convert40);
    String convert10 = ConvertUtils.convert(new Long[] {1L, 2L, 3L});
    System.out.println(convert10); // 1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    2、将字符串转换为指定数据类型对象

    // 2、将字符串转换为指定数据类型对象
    String one = "1";
    Integer convert1 = (Integer) ConvertUtils.convert(one, Integer.class);
    System.out.println(convert1); // 1
    Object convert2 = ConvertUtils.convert(one, Boolean.class);
    System.out.println(convert2); // true
    Object convert4 = ConvertUtils.convert("true", Boolean.class);
    System.out.println(convert4); // true
    Integer[] convert5 = (Integer[]) ConvertUtils.convert("[1, 2, 3]", Integer[].class);
    for (Integer integer : convert5) {
        System.out.println(integer);
    }
    // 1
    // 2
    // 3
    // 下面无法转换!!!
    // Cat convert6 = (Cat)ConvertUtils.convert("Cat(name=小猫, age=1)", Cat.class);
    // System.out.println(convert6);
    // org.apache.commons.beanutils.ConversionException: Default conversion to com.zibo.zibo2022.convert_utils.entity.Cat failed.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    3、将指定值的数组转换为指定类的对象数组(如果可能)

    // 3、将指定值的数组转换为指定类的对象数组(如果可能)
    String[] array = {"吃老鼠", "吃鱼"};
    String[] convert3 = (String[]) ConvertUtils.convert(array, String.class);
    System.out.println(Arrays.toString(convert3)); // [吃老鼠, 吃鱼]
    String[] arr = {"true", "false"};
    Boolean[] conver4 = (Boolean[]) ConvertUtils.convert(arr, Boolean.class);
    System.out.println(Arrays.toString(conver4)); // [true, false]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    4、将对象转换为指定数据类型对象

    // 4、将对象转换为指定数据类型对象
    Boolean convert7 = (Boolean) ConvertUtils.convert("true", Boolean.class);
    System.out.println(convert7); // true
    
    • 1
    • 2
    • 3

    5、基本类型转换成包装类

    // 10、基本类型转换成包装类
    int i = 1;
    Integer i2 = (Integer) ConvertUtils.convert(i, Integer.class);
    System.out.println(i2); // 1
    
    • 1
    • 2
    • 3
    • 4

    三、转换器

    1、注册自定义转换器

    转换器见准备部分!

    // 5、注册转换器
    // 这里是自定义转换器,然后注册
    ConvertUtils.register(new CatConverter(), Cat.class);
    CatConverter catConverter = new CatConverter();
    CatDto catDto = catConverter.convert(CatDto.class, cat);
    System.out.println(catDto); // CatDto(name=小猫, age=1)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2、查找指定类型的转换器

    // 6、查找指定类型的转换器
    Converter lookup = ConvertUtils.lookup(Cat.class);
    System.out.println(lookup); // com.zibo.zibo2022.convert_utils.converter.CatConverter@523884b2
    
    • 1
    • 2
    • 3

    3、查找将指定类型转换为另一种类型的转换器

    // 7、查找将指定类型转换为另一种类型的转换器
    Converter lookup2 = ConvertUtils.lookup(String.class, Boolean.class);
    System.out.println(lookup2); // ConverterFacade[BooleanConverter[UseDefault=true]]
    Converter converter = ConvertUtils.lookup(CatDto.class, Cat.class);
    System.out.println(converter); // com.zibo.zibo2022.convert_utils.converter.CatConverter@523884b2
    // 注意下面
    Converter lookup3 = ConvertUtils.lookup(Cat.class, CatDto.class);
    System.out.println(lookup3); // null
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    4、移除指定类型的转换器

    // 8、移除指定类型的转换器
    ConvertUtils.deregister(Cat.class);
    Converter converter1 = ConvertUtils.lookup(CatDto.class, Cat.class);
    System.out.println(converter1); // null
    
    • 1
    • 2
    • 3
    • 4

    5、移除所有已经注册的转换器

    // 9、移除所有已经注册的转换器
    ConvertUtils.deregister();
    
    • 1
    • 2

    四、完整代码

    package com.zibo.zibo2022.convert_utils.main;
    
    import com.zibo.zibo2022.convert_utils.converter.CatConverter;
    import com.zibo.zibo2022.convert_utils.entity.Cat;
    import com.zibo.zibo2022.convert_utils.entity.CatDto;
    import java.util.Arrays;
    import org.apache.commons.beanutils.ConvertUtils;
    import org.apache.commons.beanutils.Converter;
    
    public class Main {
    
        public static void main(String[] args) {
            Cat cat = new Cat("小猫", 1);
            Cat cat2 = new Cat("小猫2", 2);
    
            // 1、将对象转换为字符串
            String convert = ConvertUtils.convert(cat);
            System.out.println(convert); // Cat(name=小猫, age=1)
            // 下面的内容是值得注意的,如果是数组,只转换第一个元素
            String convert20 = ConvertUtils.convert(new Cat[] {cat, cat2});
            System.out.println(convert20); // Cat(name=小猫, age=1)
            String convert40 = ConvertUtils.convert(new String[] {"小猫", "1", "随便写的内容"});
            System.out.println(convert40);
            String convert10 = ConvertUtils.convert(new Long[] {1L, 2L, 3L});
            System.out.println(convert10); // 1
    
            // 2、将字符串转换为指定数据类型对象
            String one = "1";
            Integer convert1 = (Integer) ConvertUtils.convert(one, Integer.class);
            System.out.println(convert1); // 1
            Object convert2 = ConvertUtils.convert(one, Boolean.class);
            System.out.println(convert2); // true
            Object convert4 = ConvertUtils.convert("true", Boolean.class);
            System.out.println(convert4); // true
            Integer[] convert5 = (Integer[]) ConvertUtils.convert("[1, 2, 3]", Integer[].class);
            for (Integer integer : convert5) {
                System.out.println(integer);
            }
            // 1
            // 2
            // 3
            // 下面无法转换!!!
            // Cat convert6 = (Cat)ConvertUtils.convert("Cat(name=小猫, age=1)", Cat.class);
            // System.out.println(convert6);
            // org.apache.commons.beanutils.ConversionException: Default conversion to com.zibo.zibo2022.convert_utils.entity.Cat failed.
    
            // 3、将指定值的数组转换为指定类的对象数组(如果可能)
            String[] array = {"吃老鼠", "吃鱼"};
            String[] convert3 = (String[]) ConvertUtils.convert(array, String.class);
            System.out.println(Arrays.toString(convert3)); // [吃老鼠, 吃鱼]
            String[] arr = {"true", "false"};
            Boolean[] conver4 = (Boolean[]) ConvertUtils.convert(arr, Boolean.class);
            System.out.println(Arrays.toString(conver4)); // [true, false]
    
            // 4、将对象转换为指定数据类型对象
            Boolean convert7 = (Boolean) ConvertUtils.convert("true", Boolean.class);
            System.out.println(convert7); // true
    
            // 5、注册转换器
            // 这里是自定义转换器,然后注册
            ConvertUtils.register(new CatConverter(), Cat.class);
            CatConverter catConverter = new CatConverter();
            CatDto catDto = catConverter.convert(CatDto.class, cat);
            System.out.println(catDto); // CatDto(name=小猫, age=1)
    
            // 6、查找指定类型的转换器
            Converter lookup = ConvertUtils.lookup(Cat.class);
            System.out.println(lookup); // com.zibo.zibo2022.convert_utils.converter.CatConverter@523884b2
    
            // 7、查找将指定类型转换为另一种类型的转换器
            Converter lookup2 = ConvertUtils.lookup(String.class, Boolean.class);
            System.out.println(lookup2); // ConverterFacade[BooleanConverter[UseDefault=true]]
            Converter converter = ConvertUtils.lookup(CatDto.class, Cat.class);
            System.out.println(converter); // com.zibo.zibo2022.convert_utils.converter.CatConverter@523884b2
            // 注意下面
            Converter lookup3 = ConvertUtils.lookup(Cat.class, CatDto.class);
            System.out.println(lookup3); // null
    
            // 8、移除指定类型的转换器
            ConvertUtils.deregister(Cat.class);
            Converter converter1 = ConvertUtils.lookup(CatDto.class, Cat.class);
            System.out.println(converter1); // null
    
            // 9、移除所有已经注册的转换器
            ConvertUtils.deregister();
    
            // 10、基本类型转换成包装类
            int i = 1;
            Integer i2 = (Integer) ConvertUtils.convert(i, Integer.class);
            System.out.println(i2); // 1
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
  • 相关阅读:
    基于BS架构的校园爱心捐赠与物品交换平台的设计与实现 毕业设计-附源码301133
    Go 字符串处理
    Vue源码学习(三):<templete>渲染第二步,创建ast语法树
    【Linux刷题练习】
    【英雄哥六月集训】第 24天: 线段树
    weak的自动置空
    行测解题笔记完整版
    通用客户端架构
    Qt 继承QAbstractListModel实现自定义ListModel
    如何在微信公众号正文中添加附件?
  • 原文地址:https://blog.csdn.net/qq_29689343/article/details/125899183