针对前后端分离时,返回给前端的json格式的数据需要处理
- 处理规则
- String类型由null修改为返回“”
- List类型由null修改为[ ]
实现源码
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
@Aspect
@Component
public class JsonFormatAspect {
@Pointcut("execution(* com.zkane..controller.*.*(..))")
private void jsonFormat() {
}
@AfterReturning(returning = "obj", pointcut = "jsonFormat()")
public void doAfterReturning(Object obj) throws IllegalAccessException {
if (obj != null) {
formatJson(obj);
}
}
private static List<Class> classList;
static {
classList = new ArrayList<>();
classList.add(Short.class);
classList.add(Integer.class);
classList.add(Float.class);
classList.add(Long.class);
classList.add(Double.class);
classList.add(String.class);
classList.add(Boolean.class);
classList.add(Date.class);
}
private void formatJson(Object obj) throws IllegalAccessException {
if (classList.contains(obj.getClass()) || obj.getClass().isPrimitive()) {
return;
}
Field[] fields = getAllFields(obj);
for (Field field : fields) {
field.setAccessible(true);
Object value = field.get(obj);
if (String.class.isAssignableFrom(field.getType())) {
if (value == null) {
field.set(obj, "");
}
continue;
}
if (Collection.class.isAssignableFrom(field.getType()) || (value != null && value instanceof Collection)) {
if (value == null) {
field.set(obj, new ArrayList<>());
} else {
Collection collection = (Collection) value;
for (Object o : collection) {
formatJson(o);
}
}
continue;
}
if (value != null) {
formatJson(value);
}
}
}
private Field[] getAllFields(Object obj) {
List<Field> fieldList = new ArrayList<>();
Class clazz = obj.getClass();
while (clazz != null) {
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
fieldList.add(field);
}
clazz = clazz.getSuperclass();
}
Field[] fields = new Field[fieldList.size()];
fieldList.toArray(fields);
return fields;
}
}
优化后的代码
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import java.lang.reflect.Field;
import java.util.*;
/**
* 规则:String对象为null时,修改为"";集合为null时,new一个新集合(集合目前只支持List和Set)
*/
@Aspect
@Component
public class JsonFormatAspect {
@Pointcut("execution(* com.zkane..controller.*.*(..))")
private void jsonFormat() {
}
@AfterReturning(returning = "obj", pointcut = "jsonFormat()")
public void doAfterReturning(Object obj) throws Exception {
formatJson(obj);
}
private void formatJson(Object obj) throws Exception {
final String className = "com.zkane";
if (obj == null || !obj.getClass().getName().contains(className)) {
return;
}
Field[] fields = getAllFields(obj);
for (Field field : fields) {
field.setAccessible(true);
Object value = field.get(obj);
if (String.class.isAssignableFrom(field.getType())) {
if (value == null) {
field.set(obj, "");
}
continue;
}
boolean isCollection = Collection.class.isAssignableFrom(field.getType()) || (value != null && value instanceof Collection);
if (isCollection) {
if (value == null) {
if (List.class.isAssignableFrom(field.getType())) {
field.set(obj, new ArrayList());
}
if (Set.class.isAssignableFrom(field.getType())) {
field.set(obj, new HashSet());
}
} else {
Collection collection = (Collection) value;
for (Object o : collection) {
formatJson(o);
}
}
continue;
}
formatJson(value);
}
}
/**
* 获取对象的所有属性,包括父类中的属性。在获取父类时,也可以判断父类是不是自己定义的类。但是一般bean只有Object是jdk的类,Object中没有属性,所以不会影响。
*/
private Field[] getAllFields(Object obj) {
List<Field> fieldList = new ArrayList<>();
Class clazz = obj.getClass();
while (clazz != null) {
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
fieldList.add(field);
}
clazz = clazz.getSuperclass();
}
Field[] fields = new Field[fieldList.size()];
fieldList.toArray(fields);
return fields;
}
}