I have a need to filter response entity properties based on parameters provided in the request.
As an example:
GET http://example.org/api/entity/1
would respond with
{ "id": 1234, "foo": "FOO", "bar": "BAR" }
If the request contained a parameter, include=id,foo
, I want to only include the properties identified by the parameter:
{ "id": 1234, "foo": "FOO" }
In general, this type of dynamic filtering appears to be possible using Jackson filters, as documented in a few places. What I can't quite figure out is a way to provide the FilterProvider
with a reference to the HttpServletRequest
(or anything scoped like it).
ObjectMapper mapper = ...
mapper.setFilters(new FilterProvider() {
public BeanPropertyFilter findFilter(Object filterId) {
// FIXME: filter out all except those in HttpServletRequest#getParameter("include")
return SimpleBeanPropertyFilter.filterOutAllExcept(Collections.<String>emptySet());
}
});
There is a potential solution, and example, which might work if I add a servlet or Jersey filter to configure the ObjectWriterInjector
. I'm hoping there's a clearer, more direct approach.
This is all happening in a Dropwizard application, if that happens to make any difference.