0

I have two classes:

@JsonIgnoreProperties(values="foo", allowGetters = true)
public class Parent {
  String foo;
  String bar;
}

@JsonIgnoreProperties(values="alice", allowGetters = true)
public class Child extends Parent {
  String alice;
  String bob;
}

This results in a class Child where the attribute foo is not ignored (for setting). So I assume JsonIgnoreProperties of Child overrides JsonIgnoreProperties of Parent.

But I need a way to merge JsonIgnoreProperties of Child and JsonIgnoreProperties of Parent. How to do this? TIA!

t777
  • 3,099
  • 8
  • 35
  • 53
  • You can set ignoreUnknown = true which will ignore any or specify multiple using value = { "alice", "foo"} on parent – karen Nov 09 '18 at 12:21
  • @karen I do not want to ignore unknown, because I want to avoid setting some (existing) properties by the caller of a REST API. – t777 Nov 09 '18 at 12:22

2 Answers2

1

Since you've redefined the annotation in Child class it overrides the one defined in Parent.

I know 2 Ways you could solve this, both of which are not straight forward.

  1. In the Child class, instead of using @JsonIgnoreProperties make use of @JsonIgnore on the Getters of the Properties you would like to Ignore. This will make sure you're not overriding the Parent Annotation.
  2. If you still want to make use of @JsonIgnoreProperties, then Add a Custom JsonFilter and handle the annotations parsing manually in Runtime (Reflections) by looking if any of the hierarchical parent classes has a JsonIgnoreProperties then skip the assignment.
Kishore Bandi
  • 5,537
  • 2
  • 31
  • 52
0

I solved the problem with a custom BeanDeserializerModifier.

see here: Jackson Dynamic filtering of properties during deserialization

t777
  • 3,099
  • 8
  • 35
  • 53