Commit 32582570 by s_guodong

优化自定义vo赋值 和 校验

parent 18b36fb0
...@@ -69,6 +69,10 @@ public class ReflectUtil { ...@@ -69,6 +69,10 @@ public class ReflectUtil {
declaredField.set(baseVo, o); declaredField.set(baseVo, o);
} else { } else {
// 对象类型 // 对象类型
if (type == f.getType()) {
// 相同的class 直接赋值
declaredField.set(baseVo, o);
} else {
Object typeO = type.newInstance(); Object typeO = type.newInstance();
String s = setValue2RequestVo(o, f.getType(), typeO, type); String s = setValue2RequestVo(o, f.getType(), typeO, type);
if (!StringUtils.isBlank(s)) { if (!StringUtils.isBlank(s)) {
...@@ -76,6 +80,7 @@ public class ReflectUtil { ...@@ -76,6 +80,7 @@ public class ReflectUtil {
} }
declaredField.set(baseVo, typeO); declaredField.set(baseVo, typeO);
} }
}
} catch (Exception e) { } catch (Exception e) {
// Class<?> superclass = baseVoClass.getSuperclass(); // Class<?> superclass = baseVoClass.getSuperclass();
// try { // try {
......
...@@ -31,6 +31,9 @@ public class ValidatorUtil { ...@@ -31,6 +31,9 @@ public class ValidatorUtil {
CHECK_ERR_MSG.remove(); CHECK_ERR_MSG.remove();
return "参数校验错误[" + s + "]"; return "参数校验错误[" + s + "]";
} }
if (obj == null) {
return "";
}
Class<?> aClass = obj.getClass(); Class<?> aClass = obj.getClass();
Field[] fields = aClass.getDeclaredFields(); Field[] fields = aClass.getDeclaredFields();
for (Field f : fields) { for (Field f : fields) {
...@@ -41,6 +44,18 @@ public class ValidatorUtil { ...@@ -41,6 +44,18 @@ public class ValidatorUtil {
o = f.get(obj); o = f.get(obj);
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
} }
// 集合
if (type.isAssignableFrom(List.class)) {
List list = (List) o;
for (int i = 0; i < list.size(); i++) {
Object e = list.get(i);
String check = check(e);
if (StringUtils.isNotBlank(check)) {
return check;
}
}
} else if (isPrimate(type)) {
// 基本数据类型
if (f.isAnnotationPresent(Length.class)) { if (f.isAnnotationPresent(Length.class)) {
Length length = f.getAnnotation(Length.class); Length length = f.getAnnotation(Length.class);
int max = length.max(); int max = length.max();
...@@ -165,9 +180,29 @@ public class ValidatorUtil { ...@@ -165,9 +180,29 @@ public class ValidatorUtil {
return "参数校验错误[时间格式不正确]"; return "参数校验错误[时间格式不正确]";
} }
} }
} else if (type.getName().contains("com")) {
// 对象类型
String check = check(o);
if (StringUtils.isNotBlank(check)) {
return check;
}
}
} }
return ""; return "";
} }
private static boolean isPrimate(Class aclass) {
return aclass == String.class
|| aclass == BigDecimal.class
|| aclass == Date.class
|| aclass == Integer.TYPE
|| aclass == Double.TYPE
|| aclass == Float.TYPE
|| aclass == Long.TYPE
|| aclass.isPrimitive();
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment