java不返回某些字段,包括 null

一、使用 fastjson 包。

(1) SimplePropertyPreFilter 可以将需要的字段留下来。

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;

List<OffNews> list = offNewsDao.select(news); // // 需要的字段 填上 SimplePropertyPreFilter filter = new SimplePropertyPreFilter("id","heading","url","urlValid","publishTime"); String result = JSONObject.toJSONString(list,filter); List<OffNews> usa2 = JSON.parseObject(result, new TypeReference<List<OffNews>>(){});

(2)在类上打注解,。在通过将有用字符串转回对象后,其他字段均为null 了,就将所有null 字段不返回。

@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class OffNews {
 
原文地址:https://www.cnblogs.com/mobaids/p/11819448.html