Sunday, July 12, 2015

JSON Filters

In situations where you would like to filter properties from getting serialized dynamically per request Jackson provides a way through JsonFilter's

Here is an example code to create a dynamic filter


//Add the filter to the list of filters with id. In this case its "enableClientBasedFiltering"
FilterProvider filters = new SimpleFilterProvider().addFilter("enableClientBasedFiltering", theFilter);
objectMapper.setFilters(filters);
If the bean is tagged with JsonFilter Annotation example. @JsonFilter("myFilterId") , then Jackson calls the associated filter for evaluation per request

Jackson does throw an exception when filter is not found. You would have to ensure that the bean marked with JsonFilter annotation should  have the filter defined in the object mapper in its classpath

No comments:

Post a Comment