利用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报错;