通常我们在做接口开发时,常常需要接受json类型的数据,如需要先使用postMian进行测试,就需要大量的写json,这样通常需要耗费很长时间,所以本人就写了一个工具类。
//3、获取json请求参数
public static String toParam(Class clazz) {
try {
Object instance = clazz.newInstance();
Class<?> aClass = instance.getClass();
Field[] fields = copyField(aClass);//数据不可为一个死循环(继承时有可能发生)
for (Field field : fields) {
setFieldObj(field, instance);
setField(field, instance);
}
ObjectMapper mapper = new ObjectMapper();
return mapper.writeValueAsString(instance);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void setFieldObj(Field field, Object instance) throws Exception {
field.setAccessible(true);
if (validType(field.getType())) {
Class<?> type = (Class<?>) field.getGenericType();
Object o = type.newInstance();
Field[] fields1 = copyField(o.getClass());
for (Field newField : fields1) {
setFieldObj(newField, o);
setField(newField, o);
}
field.set(instance, o);
return;
}
if (field.getType().equals(List.class)) {
ParameterizedType type = (ParameterizedType) field.getGenericType();
Class<?> argument = (Class<?>) type.getActualTypeArguments()[0];
ArrayList<Object> list = new ArrayList<>();
list.add(argument.newInstance());
for (Object o : list) {
Field[] listField = copyField(o.getClass());
for (Field field1 : listField) {
setFieldObj(field1, o);
setField(field1, o);
}
}
field.set(instance, list);
return;
}
if (field.getType().equals(Map.class) || field.getType().equals(HashMap.class)) {
ParameterizedType type = (ParameterizedType) field.getGenericType();
field.set(instance, setMapVal((Class<?>) type.getActualTypeArguments()[1]));
return;
}
}
public static HashMap setMapVal(Class<?> type) {
HashMap<String, Object> map = new HashMap<>();
if (type.equals(Integer.class)) {
map.put(Symbol.KEY, 0);
return map;
}
if (type.equals(Double.class)) {
map.put(Symbol.KEY, 0d);
return map;
}
if (type.equals(Float.class)) {
map.put(Symbol.KEY, 0f);
return map;
}
if (type.equals(String.class)) {
map.put(Symbol.KEY, "");
return map;
}
map.put(Symbol.KEY,null);
return map;
}
public static void setField(Field field, Object instance) throws Exception {
field.setAccessible(true);
if (field.getType().equals(String.class)) {
field.set(instance, "");
return;
}
if (field.getType().equals(Integer.class) || field.getType().equals(int.class)) {
field.set(instance, 0);
return;
}
if (field.getType().equals(Date.class)) {
field.set(instance, null);
return;
}
}
//判断是否为包装类型
public static boolean validType(Class<?> type) {
if (type.equals(String.class) || type.equals(Set.class)) {
return false;
} else if (type.equals(Integer.class) || type.equals(int.class)) {
return false;
} else if (type.equals(BigDecimal.class) || type.equals(Date.class)) {
return false;
} else if (type.equals(Double.class) || type.equals(double.class)) {
return false;
} else if (type.equals(Boolean.class) || type.equals(boolean.class)) {
return false;
} else if (type.equals(Byte.class) || type.equals(byte.class)) {
return false;
} else if (type.equals(Short.class) || type.equals(short.class)) {
return false;
} else if (type.equals(Char.class) || type.equals(char.class)) {
return false;
} else if (type.equals(List.class) || type.equals(Map.class)) {
return false;
}else if (type.equals(HashMap.class) || type.equals(HashSet.class)) {
return false;
}
return true;
}
测试
@Test
public void test006(){
String s = BeanUtil.toParam(User2.class);
System.out.println(s);
//输出实例 {"id2":0,"name2":"","addr2":"","age":"","phone":""}
}
输出实例 {"id2":0,"name2":"","addr2":"","age":"","phone":""}
备注:支持对象字段为list类型或map类型,对象类型都可以进行格式化。多层对象解析,单不可为死循环,切记