2

I have been using codehaus for a while and the following code to exclude the fields from the JSON when serializing the same.

ObjectMapper mapper = new ObjectMapper();
mapper.getSerializationConfig().addMixInAnnotations(Object.class, PropertyFilterMixIn.class);

SimpleBeanPropertyFilter propertyFilter = SimpleBeanPropertyFilter.serializeAllExcept(ignoreFieldNames);
FilterProvider simpleFilterProvider = new    SimpleFilterProvider().addFilter("PropertyFilter", propertyFilter);

ObjectWriter writer = mapper.writer(simpleFilterProvider);
String jsonContent = writer.writeValueAsString(obj);

I upgraded to fasterxml and changed the code as follows,

ObjectMapper mapper = new ObjectMapper();
mapper.addMixIn(Object.class, PropertyFilterMixIn.class);

SimpleBeanPropertyFilter propertyFilter = SimpleBeanPropertyFilter.serializeAllExcept(ignoreFieldNames);
FilterProvider simpleFilterProvider = new SimpleFilterProvider().addFilter("PropertyFilter", propertyFilter);
//mapper.getSerializationConfig().withFilters(simpleFilterProvider);

ObjectWriter writer = mapper.writer(simpleFilterProvider);
String jsonContent = writer.writeValueAsString(obj);

However, the above code is not working as expected. It does not respect the ignoreFieldNames at all and just returns all the fields in the object (without excluding the fields mentioned in the "ignoreFieldNames" (String array).

Any help would be greatly appreciated.

Thanks in advance.

KayKay
  • 553
  • 7
  • 22
  • In this case it didnt work for me but this answer did. https://stackoverflow.com/questions/28172695/how-to-control-jackson-serialization-of-a-library-class – cabaji99 Jun 22 '22 at 14:11

1 Answers1

4

You need to annotate your bean with your filter

@JsonFilter("PropertyFilter")
public class ClassImSerializing

or in case you can't, you can add it as a global mix in annotation

ObjectMapper mapper = new ObjectMapper();
mapper.addMixIn(Object.class, PropertyFilterMixIn.class);

@JsonFilter("PropertyFilter")
class PropertyFilterMixIn {

} 
Jan Cetkovsky
  • 776
  • 6
  • 4
  • Thanks for the response. – KayKay Dec 24 '16 at 09:06
  • Thanks for the response. I tried the global mixin annotation, but it did not work. I tried the annotation approach (@JsonFilter("PropertyFilter")), it works, but it does not exclude the fields in the hierarchy, however earlier with codehaus it was removing the fields deeply in the hierarchy. – KayKay Dec 24 '16 at 09:09
  • org.springframework.http.converter.HttpMessageNotWritableException:I tried to add the annotation to sub entity classes as well, but I get the following error. Could not write content: Can not resolve PropertyFilter with id 'PropertyFilter'; no FilterProvider configured; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not resolve PropertyFilter with id 'PropertyFilter'; no FilterProvider configured – KayKay Dec 24 '16 at 09:15
  • I tried it (just in simple command line app) and it seemed to work as expected - it might be some clash with the Spring? it was along with your code FilterProvider simpleFilterProvider = new SimpleFilterProvider().addFilter("PropertyFilter", propertyFilter); where the name in the annotation and the registered filter name needs to match – Jan Cetkovsky Dec 27 '16 at 19:32