Rumah  >  Artikel  >  Java  >  Apakah kepentingan anotasi @JsonIdentityInfo menggunakan Jackson di Jawa?

Apakah kepentingan anotasi @JsonIdentityInfo menggunakan Jackson di Jawa?

WBOY
WBOYke hadapan
2023-09-23 09:37:021331semak imbas

Apakah kepentingan anotasi @JsonIdentityInfo menggunakan Jackson di Jawa?

Anotasi @JsonIdentityInfo digunakan apabila objek mempunyai hubungan ibu bapa-anak dalam perpustakaan Jackson. @JsonIdentityInfo anotasi digunakan untuk menunjukkan identiti objek semasa bersiri dan penyahsirilan. ObjectIdGenerators.PropertyGenerator ialah kelas ruang letak abstrak yang digunakan untuk mewakili kes di mana pengecam objek yang akan digunakan berasal daripada sifat POJO.

Syntax

@Target(value={ANNOTATION_TYPE,TYPE,FIELD,METHOD,PARAMETER})
@Retention(value=RUNTIME)
public @interface JsonIdentityInfo

Contoh

import java.util.*;
import java.io.*;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonIdentityInfoTest {
   public static void main(String[] args) throws IOException {
      ObjectMapper mapper = new ObjectMapper();
      User user = new User(115, "Raja", "Ramesh");
      Address address = new Address(125, "Madhapur", "Hyderabad", user);
      user.addAddress(address);
      String jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(address);
      System.out.println(jsonString);
   }
}
// User class
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "userId")
class User {
   private int userId;
   private String firstName;
   private String lastName;
   private List<Address> addresses;
   public User(int userId, String firstName, String lastName) {
      this.userId = userId;
      this.firstName = firstName;
      this.lastName = lastName;
      this.addresses = new ArrayList<Address>();
   }
   public int getUserId() {
      return userId;
   }
   public String getFirstName() {
      return firstName;
   }
   public String getLastName() {
      return lastName;
   }
   public void addAddress(Address address) {
      addresses.add(address);
   }
}
// Address class
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "userId")<strong>
</strong>class Address {
   private int userId;
   private String city;
   private String street;
   private User user;
   public Address(int userId, String street, String city, User user) {
      this.userId = userId;
      this.street = street;
      this.city = city;
      this.user = user;
   }
   public int getUserId() {
      return userId;
   }
   public String getStreet() {
      return street;
   }
   public String getCity() {
      return city;
   }
   public User getUser() {
      return user;
   }
}

Output

{
 "userId" : 125,
 "city" : "Hyderabad",
 "street" : "Madhapur",
 "user" : {
    "userId" : 115,
    "firstName" : "Raja",
    "lastName" : "Ramesh"
    }
}

Atas ialah kandungan terperinci Apakah kepentingan anotasi @JsonIdentityInfo menggunakan Jackson di Jawa?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Artikel ini dikembalikan pada:tutorialspoint.com. Jika ada pelanggaran, sila hubungi admin@php.cn Padam