Heim  >  Artikel  >  Java  >  Welche Bedeutung hat die Jackson @JsonInclude-Annotation in Java?

Welche Bedeutung hat die Jackson @JsonInclude-Annotation in Java?

WBOY
WBOYnach vorne
2023-08-19 22:01:13792Durchsuche

在Java中,Jackson @JsonInclude注解的重要性是什么?

Die Jackson @JsonInclude-Annotation kann verwendet werden, um Eigenschaften oder Felder einer Klasse unter bestimmten Bedingungen auszuschließen und kann mithilfe der JsonInclude.Include-Enumeration definiert werden. Die JsonInclude.Include-Enumeration enthält Konstanten wie "ALWAYS", "NON_DEFAULT", "NON_EMPTY" und "NON_NULL", die verwendet werden, um zu bestimmen, ob Attribute (Felder) ausgeschlossen werden sollen. Syntax

public static enum JsonInclude.Include extends Enum<JSonInclude.Include>

Beispiel

import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.*;
import java.io.*;
public class JsonIncludeTest {
   public static void main(String args[]) throws IOException {
      ObjectMapper objectMapper = new ObjectMapper();
      Employee emp = new Employee();
      String jsonString = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(emp);
      System.out.println(jsonString);
   }
}
// Employee class
@JsonInclude(JsonInclude.Include.NON_EMPTY)
class Employee {
   public int empId = 115;
   public String empName = null;
   @Override
   public String toString() {
      return "Employee{" +
             "empId=" + empId +
             ", empName=&#39;" + empName + &#39;\&#39;&#39; +
             &#39;}&#39;;
   }
}

Ausgabe

{
   "empId" : 115
}

Das obige ist der detaillierte Inhalt vonWelche Bedeutung hat die Jackson @JsonInclude-Annotation in Java?. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Dieser Artikel ist reproduziert unter:tutorialspoint.com. Bei Verstößen wenden Sie sich bitte an admin@php.cn löschen