6

I have multiple classes and for all of them I don't want id field to be the part of my output JSON string (serialization). Let say I have 2 classes

@JsonIgnoreProperties(value = { "id" })
public final class Person {
 private ObjectId id;
  //........
}

@JsonIgnoreProperties(value = { "id" })
public final class Address{
 private ObjectId id;
  //........
}

Now I don't want to specify @JsonIgnoreProperties(value = { "id" }) manually on all my 1000 classes. Is there any global way to do, so that I can apply this part for all my classes? Very similarly like mapper.setSerializationInclusion(Include.NON_NULL) in below method?

public String serialize(T dataObject) throws IOException {
        ObjectMapper mapper = new ObjectMapper();   
        mapper.setSerializationInclusion(Include.NON_NULL);
        String result = mapper.writeValueAsString(dataObject);
        return result;
}

One way I tried is to make a super class and applied @JsonIgnoreProperties on top of that (which works). But still I have to write "extends" in every child class which I don't prefer. Is there any way in which I can apply this setting without adding anything additional in my pojo class?

Shashank
  • 712
  • 15
  • 33
  • 1
    Not sure if this [How do I exclude fields with Jackson not using annotations?](https://stackoverflow.com/questions/13764280/how-do-i-exclude-fields-with-jackson-not-using-annotations) answers your question – Lino Jan 18 '19 at 08:33
  • Thanks for the reply. I just updated my description. I don't want to go to my pojo classes and add any additional setting. I am looking for something global. – Shashank Jan 18 '19 at 08:44
  • The answer in the linked question seems to apply a global filter, or have I missunderstood something? – Lino Jan 18 '19 at 08:55
  • If i am getting this right i need to apply @JsonFilter on every pojo right??? – Shashank Jan 18 '19 at 09:41
  • No it seems that you just have to add 1 single class, from the example there: `@JsonFilter("filter properties by name") class PropertyFilterMixIn {}`. I can't guarantee it though – Lino Jan 18 '19 at 10:18

1 Answers1

0

There might be more efficient solution but you might override default marshaller with the one which excludes unwanted fields but keeps all others.

raxetul
  • 439
  • 5
  • 12