簡體   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