首页 > Java > java教程 > 在Java中,我们什么时候可以调用@JsonAnyGetter和@JsonAnySetter注解?

在Java中,我们什么时候可以调用@JsonAnyGetter和@JsonAnySetter注解?

王林
发布: 2023-09-06 22:13:11
转载
1318 人浏览过

在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中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
java可以做为web的后端吗?
来自于 1970-01-01 08:00:00
0
0
0
安装JAVA
来自于 1970-01-01 08:00:00
0
0
0
无法安装java
来自于 1970-01-01 08:00:00
0
0
0
java - php调取webservice的map类型,如果封装?
来自于 1970-01-01 08:00:00
0
0
0
这个是Java语言的吗
来自于 1970-01-01 08:00:00
0
0
0
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板