@JsonFilter throws "JsonMappingException: Can not resolve BeanPropertyFilter"
I know it's already been answered but for any newcommers Jackson has actually added the ability to not fail on missing filters (JACKSON-650):
You just need to callSimpleFilterProvider.setFailOnUnknownId(false)
and you won't get this exception.
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).
EDITAlso, 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.