fastjson中使用SimplePropertyPreFilter忽略指定属性

SimplePropertyPreFilter忽略指定属性

将对象转换成json格式的时候,常常需要排除一些字段(比如密码等不能够被展示的东西)。在fastjson库中,我们可以使用SimplePropertyPreFilter忽略掉这些属性。

Student stu = new Student("name","age");
SimplePropertyPreFilter filter = new SimplePropertyPreFilter();
filter.getExcludes().add("name");
String result = JSONObject.toJSONString(stu , filter);

@JsonIgnoreProperties 忽略指定属性

@JsonIgnoreProperties("name") 忽略返回值的字段address
@Data
class Student{

	private String name; 
}

但是也说不上哪种好不好,在别人返回给你接口的对象,你就想直接过滤掉这些没用的参数的时候,你就只能用第一种了。

发布了68 篇原创文章 · 获赞 6 · 访问量 6678

猜你喜欢

转载自blog.csdn.net/renguiriyue/article/details/104359397