繁体   English   中英

在变量、getter 和 setter 上设置 @JsonProperty 有什么区别?

[英]What is the difference between @JsonProperty when set on variable, getter and setter?

我知道如果键与变量不同,JsonProperty 用于 map 变量到 JSON 键。

以下是我的示例 POJO:-

 public class JsonProperty {
        @JsonProperty("studentId")  //---------------variable
        private String id;
        
        @JsonProperty("studentId")  //---------------getter
        public String getIdValue() {
            return id;
        }
        
        @JsonProperty("studentId")  //---------------setter
        public void setIdValue(String id) {
            this.id = id;
        }
}

JSON 字符串:{“studentId”:“so1”}

假设我们使用一些 JDBC 代码保存在数据库中。 我观察到以下变化,但无法准确理解行为。

  1. 如果 @JsonProperty("studentId") 未用于变量、getter 和 setter。 观察:注意存储在数据库中。

  2. 如果 @JsonProperty("studentId") 仅用于变量。 观察: {"idValue": "so1"} 存储在数据库中。

  3. 如果在 setter 或 getter 上使用 @JsonProperty("studentId")。 观察: {"studentId": "so1"} 存储在数据库中。

那么,您能否解释一下当我们在变量、getter 和 setter 上使用 JsonProperty 时发生的实际行为是什么?

One of the simplest explanation is: @JsonProperty(name), tells Jackson ObjectMapper to map the JSON property name to the annotated Java field's name.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM