My JSON payload used to be:
{ "a": "some-value" }
And I mapped it to:
class Item {
@JsonProperty("a")
public String value;
}
Now I want to deprecate property a
and introduce a new property b
in its place. For a grace period, both shall be accepted.
@JsonAlias
seems to be the best way to achieve this:
class Item {
@JsonProperty("b")
@JsonAlias("a")
public String value;
}
It works perfect when either a
or b
are present, and when both are present and b
comes after a
:
{ "b": "some-value" }
{ "a": "some-value" }
{ "a": "this-is-ignored", "b": "uses-this-value" }
However, the properties are processed in order and Jackson will choose whichever comes last, so if the deprecated property is at the end, it wins:
{ "b": "it-should-use-this-value", "a": "but-uses-this-one-instead" }
Is there a way I can configure Jackson to always prefer the main mapping and ignore the aliases if multiple are present?
1
No. Best think you can do is custom deserializer for whole class.