0

I have a POJO class with certain properties.
I want a property to be hidden when its returned as JSON via REST api(Spring Boot).
But it should be available when I used ObjectMapper's writeValueAsBytes/readValue method.
How can I do this?

Pojo object = objectMapper.readValue(stream,Pojo.class);//should be available
objectMapper.writeValueAsBytes(pojoObject)//should write that property too
ParUpadh
  • 37
  • 1
  • 1
  • 8
  • 1
    Possible duplicate of [Only using @JsonIgnore during serialization, but not deserialization](https://stackoverflow.com/questions/12505141/only-using-jsonignore-during-serialization-but-not-deserialization) – Jaydip Rakholiya Oct 16 '18 at 18:52

1 Answers1

1

You can use SimpleBeanPropertyFilter in spring boot :

SimpleBeanPropertyFilter filter = SimpleBeanPropertyFilter.filterOutAllExcept("field2", "field3");
FilterProvider filters = new SimpleFilterProvider().addFilter("SomeBeanFilter", filter);
MappingJacksonValue mapping = new MappingJacksonValue(list);
mapping.setFilters(filters);
return mapping;

See more details at https://www.concretepage.com/jackson-api/jackson-jsonfilter-example

S.K.
  • 3,597
  • 2
  • 16
  • 31