我在过滤器中声明过滤器时遇到异常。例如,给定这些类(注意Parent有一个Child成员):
@JsonFilter("Parent")
public class Parent {
private String id;
private String name;
private Child child;
private String other1;
private String other2;
// other fields
}
@JsonFilter("Child")
public class Child {
private String id;
private String name;
// other fields
}
当我使用过滤器生成类Child
的JSON时,我没有任何问题。但是当我以这种方式使用过滤器生成类Parent
的JSON时:
ObjectMapper mapper = new ObjectMapper().setVisibility(JsonMethod.FIELD, Visibility.ANY);
String[] ignorableFieldNames = { "other1", "other2" };
FilterProvider filters = new SimpleFilterProvider().
addFilter("Parent",SimpleBeanPropertyFilter.serializeAllExcept(ignorableFieldNames));
mapper.filteredWriter(filters).writeValueAsString(object);
我收到错误No filter configured with id 'Child'
。我理解,因为Child在Parent中声明并且都有@JsonFilter
注释,所以我得到错误,因为我只使用父过滤器。但是我需要两个类中的注释,因为我也只在不同程序中的Child类上运行过滤器。什么是解决方法?
答案 0 :(得分:2)
这就是答案:您为每个带注释的过滤器附加addFilter
两次或更多次:
String[] ignorableFieldNames1 = { "other1", "other2" };
String[] ignorableFieldNames2 = { "other3", "other4" };
FilterProvider filters = new SimpleFilterProvider().
addFilter("Parent",SimpleBeanPropertyFilter.serializeAllExcept(ignorableFieldNames1))
addFilter("Child",SimpleBeanPropertyFilter.serializeAllExcept(ignorableFieldNames2));