Jackson序列化对象的过滤

参数选项

  • Include.NON_DEFAULT 属性为默认值不序列化
  • Include.NON_EMPTY 属性为 空(“”) 或者为 NULL 都不序列化
  • Include.NON_NULL 属性为NULL 不序列化

方法过滤

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
/**
 * jackson to fastjson
 * 
 * @param obj
 * @return
 */
public static Object NON_DEFAULT(Object obj) {
		try {
			obj = new ObjectMapper().setSerializationInclusion(Include.NON_DEFAULT).writeValueAsString(obj);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		return obj;
	}

注解过滤

1
2
3
4
5
6
7
import com.fasterxml.jackson.annotation.JsonInclude;
public class APIResultBean<T> implements Serializable{
	private static final long serialVersionUID = 1L;
	// 过滤空值
	@JsonInclude(JsonInclude.Include.NON_DEFAULT)
	private T data;
}

今日诗词

作者信息