> Java > java지도 시간 > 본문

Jackson을 사용하여 Java에서 필드의 대체 이름을 정의하는 방법은 무엇입니까?

王林
풀어 주다: 2023-08-27 13:49:05
앞으로
798명이 탐색했습니다.

Jackson을 사용하여 Java에서 필드의 대체 이름을 정의하는 방법은 무엇입니까?

@JsonAlias 주석은 역직렬화 중에 허용되는 속성에 대해 하나 이상의 대체 이름을 정의하여 JSON 데이터를 Java 개체로 설정할 수 있습니다. 그러나 직렬화할 때(예: Java 객체에서 JSON을 가져오는 경우) alias.

Syntax

<strong>@Target(value={ANNOTATION_TYPE,FIELD,METHOD,PARAMETER})
@Retention(value=RUNTIME)
public @interface JsonAlias</strong>
로그인 후 복사

Example

적中文翻译为:

示例

import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
import java.io.*;
public class ObjectToJsonTest {
   public static void main(String[] args) throws JsonProcessingException {
      <strong>ObjectMapper </strong>mapper = new <strong>ObjectMapper()</strong>;
      Technology tech = new Technology("Java", "Oracle");
      Employee emp = new Employee(110, "Raja", tech);
      String jsonWriter = mapper.<strong>writerWithDefaultPrettyPrinter().writeValueAsString(emp);</strong>
      System.out.println(jsonWriter);
   }
}
<strong>// Technology class
</strong>class Technology {
<strong>   @JsonProperty("skill")
</strong>   private String skill;
<strong>   @JsonProperty("subSkill")
</strong><strong>   @JsonAlias({"sSkill", "mySubSkill"})
</strong>   private String subSkill;
   public Technology(){}
   public Technology(String skill, String subSkill) {
      this.skill = skill;
      this.subSkill = subSkill;
   }
   public String getSkill() {
      return skill;
   }
   public void setSkill(String skill) {
      this.skill = skill;
   }
   public String getSubSkill() {
      return subSkill;
   }
   public void setSubSkill(String subSkill) {
      this.subSkill = subSkill;
   }
}
<strong>// Employee class
</strong>class Employee {
<strong>   @JsonProperty("empId")
</strong>   private Integer id;
<strong>   @JsonProperty("empName")
</strong><strong>   @JsonAlias({"ename", "myename"})
</strong>   private String name;
<strong>   @JsonProperty("empTechnology")
</strong>   private Technology tech;
   public Employee(){}
   public Employee(Integer id, String name, Technology tech){
      this.id = id;
      this.name = name;
      this.tech = tech;
   }
   public Integer getId() {
      return id;
   }
   public void setId(Integer id) {
      this.id = id;
   }
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
   public Technology getTechnology() {
      return tech;
   }
   public void setTechnology(Technology tech) {
      this.tech = tech;
   }
}
로그인 후 복사

输流

<strong>{
 "technology" : {
 "skill" : "Java",
 "subSkill" : "Oracle"
 },
 "empId" : 110,
 "empName" : "Raja",
 "empTechnology" : {
 "skill" : "Java",
 "subSkill" : "Oracle"
 }
}</strong>
로그인 후 복사
대신 실제 논리 속성 이름만 사용됩니다.

위 내용은 Jackson을 사용하여 Java에서 필드의 대체 이름을 정의하는 방법은 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:tutorialspoint.com
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!