/**
* 检测参数 将Null替换为""
*/
public static Object checkObjectParam1(Object entity) {
Field[] fields = entity.getClass().getDeclaredFields();
for(Field field : fields) {
field.setAccessible(true);
try {
Object objValue = field.get(entity);
if(MyStringUtils.isEmpty(objValue)) {
field.set(entity, "");
}
} catch (Exception e) {
e.printStackTrace();
}
}
return entity;
}
public static Object checkObjectParam2(Object entity) throws Exception {
Field[] fields = entity.getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
Type fieldType = field.getGenericType();
//判断当前字段是否属于基本类型
if (fieldType.toString().equals("class java.lang.String") || fieldType.toString().equals("class java.lang.Integer")) {
Object objValue = field.get(entity);
if (MyStringUtils.isEmpty(objValue)) {
//将Null替换为""
field.set(entity, "");
}
//如果为其他类型继续判断子类
} else if (fieldType.toString().contains("class com.sunyard.py.pojo")) {
Object children = field.get(entity);
if (children == null) {
Class<?> currentClass = Class.forName(fieldType.toString().replace("class ", ""));
children = currentClass.newInstance();
}
//递归
checkObjectParam(children);
field.set(entity, children);
}
}
return entity;
}
检测返回参数 将Null替换为空字符串
最新推荐文章于 2024-04-30 17:28:01 发布