Jackson: What happens if a property is missing?

后端 未结 6 834
北恋
北恋 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:17

    Summarizing excellent answers by Programmer Bruce and StaxMan:

    1. Missing properties referenced by the constructor are assigned a default value as defined by Java.

    2. You can use setter methods to differentiate between properties that are implicitly or explicitly set. Setter methods are only invoked for properties with explicit values. Setter methods can keep track of whether a property was explicitly set using a boolean flag (e.g. isValueSet).

提交回复
热议问题