3 回答
TA贡献1853条经验 获得超9个赞
下面的示例按名称排除字段来自我的博客文章Gson v Jackson-第4部分。(搜索了PropertyFilterMixIn。)这个例子演示了使用FilterProvider带有一个SimpleBeanPropertyFilter以serializeAllExcept字段名的用户指定的列表。
@JsonFilter("filter properties by name")
class PropertyFilterMixIn {}
class Bar
{
public String id = "42";
public String name = "Fred";
public String color = "blue";
public Foo foo = new Foo();
}
class Foo
{
public String id = "99";
public String size = "big";
public String height = "tall";
}
public class JacksonFoo
{
public static void main(String[] args) throws Exception
{
ObjectMapper mapper = new ObjectMapper();
mapper.getSerializationConfig().addMixInAnnotations(
Object.class, PropertyFilterMixIn.class);
String[] ignorableFieldNames = { "id", "color" };
FilterProvider filters = new SimpleFilterProvider()
.addFilter("filter properties by name",
SimpleBeanPropertyFilter.serializeAllExcept(
ignorableFieldNames));
ObjectWriter writer = mapper.writer(filters);
System.out.println(writer.writeValueAsString(new Bar()));
// output:
// {"name":"James","foo":{"size":"big","height":"tall"}}
}
}
(注意:在最新的Jackson版本中,相关的API可能已稍作更改。)
尽管该示例确实使用了看似不必要的注释,但该注释并未应用于要排除的字段。(为帮助更改API,以简化必要的配置,请不要犹豫,为问题JACKSON-274的实现投票。
TA贡献1862条经验 获得超7个赞
我编写了一个库来处理类似的用例。我需要根据用户请求数据以编程方式忽略字段。普通的Jackson选项太笨拙了,我讨厌它使我的代码看起来很奇怪。
该库使这一切更容易理解。它允许您简单地执行此操作:
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.monitorjbl.json.JsonView;
import com.monitorjbl.json.JsonViewSerializer;
import static com.monitorjbl.json.Match.match;
//initialize jackson
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addSerializer(JsonView.class, new JsonViewSerializer());
mapper.registerModule(module);
//get a list of the objects
List<MyObject> list = myObjectService.list();
String json;
if(user.getRole().equals('ADMIN')){
json = mapper.writeValueAsString(list);
} else {
json = mapper.writeValueAsString(JsonView.with(list)
.onClass(MyObject.class, match()
.exclude("*")
.include("name")));
}
System.out.println(json);
该代码可在GitHub上获得,希望对您有所帮助!
TA贡献1860条经验 获得超9个赞
如果您在两个或多个pojo上定义了过滤器,则可以尝试以下操作:
@JsonFilter("filterAClass")
class AClass
{
public String id = "42";
public String name = "Fred";
public String color = "blue";
public int sal = 56;
public BClass bclass = new BClass();
}
//@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
@JsonFilter("filterBClass")
class BClass
{
public String id = "99";
public String size = "90";
public String height = "tall";
public String nulcheck =null;
}
public class MultipleFilterConcept {
public static void main(String[] args) throws Exception
{
ObjectMapper mapper = new ObjectMapper();
// Exclude Null Fields
mapper.setSerializationInclusion(Inclusion.NON_NULL);
String[] ignorableFieldNames = { "id", "color" };
String[] ignorableFieldNames1 = { "height","size" };
FilterProvider filters = new SimpleFilterProvider()
.addFilter("filterAClass",SimpleBeanPropertyFilter.serializeAllExcept(ignorableFieldNames))
.addFilter("filterBClass", SimpleBeanPropertyFilter.serializeAllExcept(ignorableFieldNames1));
ObjectWriter writer = mapper.writer(filters);
System.out.println(writer.writeValueAsString(new AClass()));
}
}
添加回答
举报