Jackson: What happens if a property is missing?

后端 未结 6 851
北恋
北恋 2020-12-01 02:01

What happens if I annotate a constructor parameter using @JsonProperty but the Json doesn\'t specify that property. What value does the constructor get?

6条回答
  •  南方客
    南方客 (楼主)
    2020-12-01 02:13

    In addition to constructor behavior explained in @Programmer_Bruce's answer, one way to differentiate between null value and missing value is to define a setter: setter is only called with explicit null value. Custom setter can then set a private boolean flag ("isValueSet" or whatever) if you want to keep track of values set.

    Setters have precedence over fields, in case both field and setter exist, so you can "override" behavior this way as well.

提交回复
热议问题