Computer >> 컴퓨터 >  >> 프로그램 작성 >> Java

Java에서 @JsonAnyGetter 및 @JsonAnySetter 주석을 언제 호출할 수 있습니까?


@JsonAnyGetter 주석 지도 사용 가능 컨테이너 로 JSON 및 @JsonAnySetter로 직렬화하려는 속성의 경우 주석 모든 인식할 수 없는 필드에 대해 동일한 setter 메서드를 호출하도록 Jackson에 지시합니다. JSON 개체에서, 이는 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")
   private String firstName;
   @JsonProperty("EMPLOYEE_LAST_NAME")
   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;
   }
   @JsonAnyGetter
   public Map<String, String> getAdditionalInformation() {
      return additionalInformation;
   }
   public void setAdditionalInformation(Map<String, String> additionalInformation) {
      this.additionalInformation = additionalInformation;
   }
   @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}