利用commons-beanutils.jar将对象为空的属性设置为null

利用commons-beanutils.jar将对象为空的属性设置为null

代码

pom.xml

<dependencies>
        <!-- test -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <!-- 日志 -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.20</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.25</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.35</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils -->
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.9.3</version>
        </dependency>

    </dependencies>

Student.java

package com.ydfind.object.model;

import lombok.Data;

@Data
public class Student {

    private Integer id;

    private Integer age;

    private String name;

    private String address;

    private String phone;

    private String sex;

    public Student(){

    }

    public Student(Integer id, Integer age, String name, String address, String phone, String sex){
        this.id = id;
        this.name = name;
        this.age = age;
        this.address = address;
        this.phone = phone;
        this.sex = sex;
    }
}

Student1.java

package com.ydfind.object.model;

public class Student1 {

    private Integer id;

    private Integer age;

    private String name;

    private String address;

    private String phone;

    private String sex;

    public Student1(){

    }

    public Student1(Integer id, Integer age, String name, String address, String phone, String sex){
        this.id = id;
        this.name = name;
        this.age = age;
        this.address = address;
        this.phone = phone;
        this.sex = sex;
    }

    public void setName(String name){
        this.name = name;
    }

    public String getName(){
        return this.name;
    }

    public void setAddress(String address){
        this.address = address;
    }

    public void getPhone(String phone){
        this.phone = phone;
    }
}

ObjectExample.java

package com.ydfind.object;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.PropertyUtils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Type;

@Slf4j
public class ObjectExample {

    /**
     * 对象中字符串属性,若为空则设置为null
     * @param obj
     * @throws IllegalAccessException
     * @throws NoSuchMethodException
     * @throws InvocationTargetException
     */
    public static void changeStringToNull(Object obj) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        for (Class<?> clazz = obj.getClass(); clazz != Object.class; clazz = clazz.getSuperclass()) {

            Field fields[] = clazz.getDeclaredFields();

            // 遍历属性值,取得所有属性为 null 值的
            for (Field field : fields) {
                Type t = field.getGenericType();
                if (t.getTypeName().equals(String.class.getTypeName())) {
                    Object property = PropertyUtils.getProperty(obj, field.getName());
                    if (property != null && "".equals(String.valueOf(property).trim())) {
                        log.info("将设置{}为null", field.getName());
                        PropertyUtils.setProperty(obj, field.getName(), null);
                    }
                }
            }
        }
    }

}

ObjectExampleTest.java

package com.ydfind.object;

import com.ydfind.object.model.Student;
import com.ydfind.object.model.Student1;
import org.junit.Test;

import java.lang.reflect.InvocationTargetException;

public class ObjectExampleTest {

    @Test
    public void testChangeStringToNull() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        // public Student(Integer id, Integer age, String name, String address, String phone, String sex)
        Student student = new Student(1, 18, "张三", "", " ", "男");
        System.out.println("student define: " + student);
        ObjectExample.changeStringToNull(student);
        System.out.println("student process: " + student);
        Student1 student1 = new Student1(1, 18, " ", "", " ", "男");
        System.out.println("student1 define: " + student1);
        ObjectExample.changeStringToNull(student1);
        System.out.println("student1 process: " + student1);

    }
}

运行

在这里插入图片描述
可以看到Student对象可以正常运行,但address属性因为没有get方法,导致PropertyUtils.getProperty报错;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值