Abstract
The student provides a JAR, and then the bean inside is 4 level nested, and then there is no document. It’s a little confusing looking at so many properties. Accustomed to the document format of Swaggger, I wonder if there is a way to achieve this requirement. Did you look it up online? Did it yourself.
The keyword
Reflect one-key initialization beans, serialization preserves multiple levels of NULL
1. Background
The student provides a JAR, and then the bean inside is 4 level nested, there is no document. It’s a little confusing looking at so many properties. Accustomed to the document format of Swaggger, I wonder if there is a way to achieve this requirement
2. The nature of the problem
- Display the structure of a bean as JSON
3, the difficulties in
- The bean structure is multi-level
- Beans contain custom classes
- Beans contain static classes, inner classes
4. Possible implementation
4.1. Use tripartite serialization tools, such as Gson implementation
XXXTaskAddInput xxxTaskAddInput=new XXXTaskAddInput();
Gson gson=new GsonBuilder().serializeNulls().create();
System.out.println(gson.toJson(xxxTaskAddInput));
Copy the code
The results of
{
"groupId": null,
"groupName": null,
"tasks": null
}
Copy the code
As you can clearly see from the result, only the level 1 null attribute is preserved. Therefore, Gson’s serializeNulls function does not meet my needs.
4.2. Focus on initialization
It’s easy to see that the problem is not serialization, but bean initialization. Because the first level attributes are all null. Secondary attributes naturally cannot be serialized. So our core is to implement the variable bean and assign the default values of the corresponding types to the properties at all levels.
Checked next baidu simply, do not have direct usable, began oneself next. Reflection is the key to this problem.
Reusable tool classes
public static void setFeidValueNotNull(Object obj) throws Exception { for (Field field : obj.getClass().getDeclaredFields()) { field.setAccessible(true); if (field.get(obj) == null || field.get(obj).toString().equals("[]")) { if (field.getGenericType().toString().equals("class java.lang.String")) { field.set(obj, ""); } else if (field.getGenericType().toString().equals("class java.lang.Integer")) { field.set(obj, 0); } else if (field.getGenericType().toString().equals("class java.lang.double ")) {field.set(obj, 0.0); } else if (field.getGenericType().toString().equals("class java.lang.Long")) { field.set(obj, 0L); } else { Type type = field.getGenericType(); if (List.class.isAssignableFrom(field.getType())) { List arraylist = new ArrayList(); // Check whether type is a parameterized type. For example, Collection<String> is a parameterized type. If (type instanceof ParameterizedType) {// Get the type parameter type of the type. See the JDK help documentation for an explanation of ParameterizedType. Class clazz = (Class) ((ParameterizedType) type).getActualTypeArguments()[0]; Object subObject = clazz.newInstance(); setFeidValueNotNull(subObject); arraylist.add(subObject); } field.set(obj, arraylist); } else { Class clazz = Class.forName(field.getGenericType().getTypeName()); Object subObject = clazz.newInstance(); setFeidValueNotNull(subObject); field.set(obj, subObject); } } } } }Copy the code
How to use
XXXTaskAddInput xxxTaskAddInput=new XXXTaskAddInput();
setFeidValueNotNull(xxxTaskAddInput);
System.out.println(new Gson().toJson(xxxTaskAddInput));
Copy the code
The effect
{
"groupId": "",
"groupName": "",
"tasks": [
{
"bizId": "",
"bizInfo": {
"receiver": "",
"extendInfo": "",
"organizationId": "",
"exportInfos": [
{
"columnIndex": 0,
"rowspan": 1,
"columnName": "",
"columnValue": ""
}
],
"result": ""
},
"name": "",
"type": 0,
"total": 0,
"accountOid": "",
"accountGid": "",
"receiver": ""
}
]
}
Copy the code
Isn’t swagger less complicated than it looks? By the way, swagger is made with notes.
6, summary
Find the problem, solve the problem, summarize the problem. Take it one step at a time. Looking back, the so-called difficulties are not so difficult.