" および "isGetterVisibility" を設定できます。JsonAutoDetect クラスは、Java クラスの可視性レベルと同様のパブリック静的定数を定義できます。 ANY ”、”DEFAULT”、”NON_PRIVATE”、”NONE”、”PROTEC”"/> " および "isGetterVisibility" を設定できます。JsonAutoDetect クラスは、Java クラスの可視性レベルと同様のパブリック静的定数を定義できます。 ANY ”、”DEFAULT”、”NON_PRIVATE”、”NONE”、”PROTEC”">

ホームページ  >  記事  >  Java  >  Java で @JsonAutoDetect アノテーションを使用する場合は?

Java で @JsonAutoDetect アノテーションを使用する場合は?

WBOY
WBOY転載
2023-09-01 18:01:06737ブラウズ

Java で @JsonAutoDetect アノテーションを使用する場合は?

@JsonAutoDetect アノテーション クラス レベルで使用して、 シリアル化中にクラス プロパティの 可視性 をオーバーライドできます を逆シリアル化します。 "creatorVisibility"、"fieldVisibility"、"getterVisibility"、"setterVisibility" などのプロパティを使用して、可視性を設定できます>" JsonAutoDetect クラスは、Java クラスと同様の パブリック静的定数を定義できます。" ANY などの可視性レベル"、"DEFAULT"、"NON_PRIVATE"、"NONE"、"PROTECTED_AND_PRIVATE" および "PUBLIC_ONLY"。 Example

import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.*;
import java.io.*;
public class JsonAutoDetectTest {
   public static void main(String[] args) throws IOException {
      Address address = new Address("Madhapur", "Hyderabad", "Telangana");
      Name name = new Name("Raja", "Ramesh");
      Student student = new Student(address, name, true);
      ObjectMapper mapper = new ObjectMapper();
      String jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(student);
      System.out.println("JSON: " + jsonString);
   }
}
// Address class
class Address {
   private String firstLine;
   private String secondLine;
   private String thirdLine;
   public Address(String firstLine, String secondLine, String thirdLine) {
      this.firstLine = firstLine;
      this.secondLine = secondLine;
      this.thirdLine = thirdLine;
   }
   public String getFirstLine() {
      return firstLine;
   }
   public String getSecondLine() {
      return secondLine;
   }
   public String getThirdLine() {
      return thirdLine;
   }
}
// Name class
class Name {
   private String firstName;
   private String secondName;
   public Name(String firstName, String secondName) {
      this.firstName = firstName;
      this.secondName = secondName;
   }
   public String getFirstName() {
      return firstName;
   }
   public String getSecondName() {
      return secondName;
   }
}
// Student class
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
class Student {
   private Address address;
   private Name name;
   private Boolean isActive;
   public Student(Address address, Name name, Boolean isActive) {
      this.address = address;
      this.name = name;
      this.isActive = isActive;
   }
}

出力

{
 "address" : {
    "firstLine" : "Madhapur",
    "secondLine" : "Hyderabad",
    "thirdLine" : "Telangana"
 },
 "name" : {
    "firstName" : "Raja",
    "secondName" : "Ramesh"
 },
 "isActive" : true
}<strong>
</strong>

以上がJava で @JsonAutoDetect アノテーションを使用する場合は?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はtutorialspoint.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。