Reputation: 1071
I'm getting an exception when a filter is declared within a filter. For example, given these classes (note that Parent has a Child member):
@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
}
When I generate JSON of class Child
using filters I have no problems. But when I generate JSON of class Parent
using filter this way:
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);
I get the error No filter configured with id 'Child'
. I understand that since Child is declared in Parent and both have the @JsonFilter
annotation, I get the error because I only use the Parent filter. But I need the annotation in both classes, as I also run the filter only on the Child class in a different program. What's the workaround?
Upvotes: 1
Views: 1235
Reputation: 1071
This is the answer: you append addFilter
two or more times for each annotated filter:
String[] ignorableFieldNames1 = { "other1", "other2" };
String[] ignorableFieldNames2 = { "other3", "other4" };
FilterProvider filters = new SimpleFilterProvider().
addFilter("Parent",SimpleBeanPropertyFilter.serializeAllExcept(ignorableFieldNames1))
addFilter("Child",SimpleBeanPropertyFilter.serializeAllExcept(ignorableFieldNames2));
Upvotes: 2