SerializerFeature.WriteNullListAsEmpty , // List字段如果为null,输出为[],而非null
SerializerFeature.WriteNullStringAsEmpty, //字符类型字段如果为null,输出为”“,而非null
SerializerFeature.WriteDateUseDateFormat, //如果时间是data、时间戳类型,按照这种格式初始化时间 "yyyy-MM-dd HH:mm"
SerializerFeature.WriteNullNumberAsZero, //数值字段如果为null,输出为0,而非null
SerializerFeature.WriteNullBooleanAsFalse ,//Boolean字段如果为null,输出为false,而非null
SerializerFeature.SkipTransientField , //类中的Get方法对应的Field是transient,序列化时将会被忽略。默认为true
SerializerFeature.DisableCircularReferenceDetect //消除对同一对象循环引用的问题,默认为false
案例
package com.cn.dl;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.sql.Timestamp;
import java.util.Date;
/**
* Created by yanshao on 2018/11/26.
*/
@Data
@NoArgsConstructor
@Builder
@AllArgsConstructor
public class Stu {
private String name;
private Integer age;
private Boolean isMan;
private String address = "beijing";
private transient String interest = "ball";
private Date createDate = new Date();
}
package com.cn.dl;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.PropertyFilter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.junit.Test;
public class SpringbootAopTestApplicationTests {
@Test
public void contextLoads() {
Stu stu = new Stu();
System.out.println(toJSON(stu));
System.out.println(toJSON2(stu));
System.out.println(toJSON3(stu));
}
/**
* 过滤字段值等于null的字段
* */
private PropertyFilter filter = new PropertyFilter() {
/**
* @param obj 当前对象
* @param key 当前对象属性
* @param value 当前对象属性值
* @return
* */
@Override
public boolean apply(Object obj, String key, Object value) {
if(value != null){
return true;
}
return false;
}
};
/**
* 过滤字段值等于null的字段
* */
private PropertyFilter nullFilter = (obj, key,value) -> {
if (value != null){
return true;
}
return false;
};
private String toJSON(Object obj){
return JSONObject.toJSONString(
obj,
SerializerFeature.WriteNullListAsEmpty,
SerializerFeature.WriteNullStringAsEmpty,
SerializerFeature.WriteDateUseDateFormat,
SerializerFeature.WriteNullNumberAsZero,
SerializerFeature.WriteNullBooleanAsFalse,
SerializerFeature.DisableCircularReferenceDetect
);
}
private String toJSON2(Object obj){
return JSONObject.toJSONString(
obj,
filter,
SerializerFeature.WriteNullListAsEmpty,
SerializerFeature.WriteNullStringAsEmpty,
SerializerFeature.WriteDateUseDateFormat,
SerializerFeature.WriteNullNumberAsZero,
SerializerFeature.WriteNullBooleanAsFalse,
SerializerFeature.DisableCircularReferenceDetect
);
}
private String toJSON3(Object obj){
return JSONObject.toJSONString(
obj,
nullFilter,
SerializerFeature.WriteNullListAsEmpty , // List字段如果为null,输出为[],而非null
SerializerFeature.WriteNullStringAsEmpty, //字符类型字段如果为null,输出为”“,而非null
SerializerFeature.WriteDateUseDateFormat, //如果时间是data、时间戳类型,按照这种格式初始化时间 "yyyy-MM-dd HH:mm"
SerializerFeature.WriteNullNumberAsZero, //数值字段如果为null,输出为0,而非null
SerializerFeature.WriteNullBooleanAsFalse ,//Boolean字段如果为null,输出为false,而非null
SerializerFeature.SkipTransientField , //类中的Get方法对应的Field是transient,序列化时将会被忽略。默认为true
SerializerFeature.DisableCircularReferenceDetect //消除对同一对象循环引用的问题,默认为false
);
}
}
/**
* 过滤字段值等于null的字段
* */
private PropertyFilter nullFilter = (obj, key,value) -> {
if (value != null){
return true;
}
return false;
};
注意点:
1、这个方法用来过滤对象obj中为null的字段,其中key是字段名,value是字段值;
2、如果javaBean上的字段用transient修饰,就会忽略掉这个字段;
测试结果:
{"address":"beijing","age":0,"createDate":"2018-11-27 10:15:06","isMan":false,"name":""}
{"address":"beijing","createDate":"2018-11-27 10:15:06"}
{"address":"beijing","createDate":"2018-11-27 10:15:06"}