Jackson JSON filter without @JsonFilter Abstract

I am trying to filter out the API endpoint response using the Jackson library.

I can use @JsonFilter ("[filterNameHere]"), but this ends with a class expecting the filter to be applied every time.

Is there a way to filter the response on only one specific instance?

Pizza pizza = pizzaService.getPizzaById(pizzaId);

ObjectMapper mapper = new ObjectMapper();
FilterProvider filters = new SimpleFilterProvider().addFilter("baseFilter", SimpleBeanPropertyFilter.serializeAllExcept("base"));

String json = mapper.writer(filters).writeValueAsString(pizza);

return Response.status(200).entity(json).build();

I tried to look back, but could not find anything suitable.

+4
source share
1 answer

Unfortunately, the association between the filter id and the class is static, so any class has or does not; that is, you cannot sometimes have a filter, while others cannot.

However, it is also possible:

  • ( SimpleFilterProvider.setDefaultFilter()), , , . " " , .
  • , , , : SimpleFilterProvider.setFailOnUnknownId(false) ( )

, , , :

SimpleFilterProvider filters = new SimpleFilterProvider();
filters.setFailOnUnknownId(false);
String json = mapper.writer(filters).writeValueAsString(pizza);
+5

All Articles