首頁 >Java >java教程 >在Java中,我們什麼時候可以呼叫@JsonAnyGetter和@JsonAnySetter註解?

在Java中,我們什麼時候可以呼叫@JsonAnyGetter和@JsonAnySetter註解?

王林
王林轉載
2023-09-06 22:13:111183瀏覽

在Java中,我們什麼時候可以呼叫@JsonAnyGetter和@JsonAnySetter註解?

@JsonAnyGetter 註解允許使用Map作為我們想要序列化為 JSON 和 @JsonAnySetter 的屬性的容器#annotation 指示Jackson在JSON物件中的所有未識別欄位上呼叫相同的setter方法,這表示所有未對應到Java物件中的屬性或setter方法的欄位。

語法

public @interface JsonAnyGetter
public @interface JsonAnyGetter

範例

import java.io.*;
import java.util.*;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.annotation.*;
public class JsonAnyGetterAndJsonAnySetterTest {
   public static void main(String args[]) throws JsonGenerationException, JsonMappingException, IOException {
      Employee emp1 = new Employee();
      emp1.setFirstName("Adithya");
      emp1.setLastName("Sai");
      emp1.setEmpId(125);
      emp1.getAdditionalInformation().put("technology1", "Machine Learning");
      emp1.getAdditionalInformation().put("technology2", "Robotics");
      ObjectMapper mapper = new ObjectMapper();
      String jsonStr = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(emp1);
      System.out.println(jsonStr);
      System.out.println("Deserializing JSON to Object:");
      Employee emp2 = mapper.readValue(jsonStr, Employee.class);
      System.out.println("id : " + emp2.getEmpId());
      System.out.println("firstName : " + emp2.getFirstName());
      System.out.println("lastName : " + emp2.getLastName());
      System.out.println("Additional information : " + emp2.getAdditionalInformation());
   }
}
// Employee class
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({"firstName", "lastName", "technologies", "empId" })
class Employee {
   @JsonProperty("EMPLOYEE_ID")
   private int empId;
   @JsonProperty("EMPLOYEE_FIRST_NAME")<strong>
</strong>   private String firstName;
<strong>  </strong> @JsonProperty("EMPLOYEE_LAST_NAME")<strong>
</strong>   private String lastName;
   private Map<String, String> additionalInformation = new HashMap<>();
   public int getEmpId() {
      return empId;
   }
   public void setEmpId(int empId) {
      this.empId = empId;
   }
   public String getFirstName() {
      return firstName;
   }
   public void setFirstName(String firstName) {
      this.firstName = firstName;
   }
   public String getLastName() {
      return lastName;
   }
   public void setLastName(String lastName) {
      this.lastName = lastName;
   }
<strong>  </strong> @JsonAnyGetter<strong>
</strong>   public Map<String, String> getAdditionalInformation() {
      return additionalInformation;
   }
   public void setAdditionalInformation(Map<String, String> additionalInformation) {
      this.additionalInformation = additionalInformation;
   }
<strong>   </strong>@JsonAnySetter
   public void setAdditionalProperty(final String name, final String value) {
      this.additionalInformation.put(name, value);
   }
}

輸出

{
  "EMPLOYEE_FIRST_NAME" : "Adithya",
  "EMPLOYEE_LAST_NAME" : "Sai",
  "EMPLOYEE_ID" : 125,
  "technology1" : "Machine Learning",
  "technology2" : "Robotics"
}
Deserializing JSON to Object:
id : 125
firstName : Adithya
lastName : Sai
Additional information : {technology1=Machine Learning, technology2=Robotics}

以上是在Java中,我們什麼時候可以呼叫@JsonAnyGetter和@JsonAnySetter註解?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除