fastjson之serializer.SimplePropertyPreFilter
需要根据不同的环境返回定制化返回属性时,可以使用SimplePropertyPreFilter
自定义实体类
public class Area {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@Column(name="name",length=100)
private String name;
@Column(name = "code", length = 20)
private String code;
//省略了get,set方法和构造方法
}
测试类
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
public void test() {
/*1.获取区域表获取数据,以列表返回 findAreaChild()自定义方法*/
List<Area> areas = areaService.findAreaChild(areaid);
/*2.定制序列化 只序列化id和名称*/
SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Area.class, "id", "name");
/*3.JSONObject.toJSONString() 将JavaBean序列化为JSON文本*/
System.out.println(JSONObject.toJSONString(areas, filter));
}