Is it possible to selectively determine when the @JsonFilter annotation gets used at runtime?
I\'m getting JsonMappingException exception (see below) when I don\'t p
I think you could trick the filtered writer defining an empty serialize filter for the cases where you want all the properties seralized:
FilterProvider filters = new SimpleFilterProvider().addFilter("apiFilter", SimpleBeanPropertyFilter.serializeAllExcept(emptySet));
This way, when the engine looks for the "apiFilter" filter defined at the @JsonFilter anotation, it finds it, but it will not have any effect (as will serialize all the properties).
EDIT
Also, you can call the factory method writer() instead of filteredWriter():
ObjectWriter writer=null;
if(aplyFilter) {
FilterProvider filters = new SimpleFilterProvider().addFilter("apiFilter", SimpleBeanPropertyFilter.filterOutAllExcept(filterProperties));
writer=mapper.filteredWriter(filters);
} else {
writer=mapper.writer();
}
return writer.writeValueAsString(user);
I think this last solution is way cleaner, and indeed better.