原因为:使用BeanUtils时,有些类型值为空,例如Date。
若为Date:
- 1.设置默认值
如 ConvertUtils.register(new DateConverter(null), java.util.Date.class);
或在get/set方法中设置
- 2.执行BeanUtils.copyProperties()方法前,判断对象属性值是否为空
if (!(CheckObjAllFieldsIsNull.checkObjAllFieldsIsNull(object))) {...}
public class CheckObjAllFieldsIsNull {
/**
* 判断对象中属性值是否全为空
*
* @param object
* @return
*/
public static boolean checkObjAllFieldsIsNull(Object object) {
if (null == object) {
return true;
}
try {
for (Field f : object.getClass().getDeclaredFields()) {
f.setAccessible(true);
System.out.print(f.getName() + ":");
System.out.println(f.get(object));
if (f.get(object) != null && StringUtils.isNotBlank(f.get(object).toString())) {
return false;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
}