Home  >  Article  >  Java  >  What is the importance of Jackson @JsonInclude annotation in Java?

What is the importance of Jackson @JsonInclude annotation in Java?

WBOY
WBOYforward
2023-08-19 22:01:13866browse

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

The Jackson @JsonInclude annotation can be used to exclude properties or fields of a class under certain conditions, and It can be defined using the JsonInclude.Include enumeration. JsonInclude.Include enum Contains some constants, such as "ALWAYS", "NON_DEFAULT", "NON_EMPTY" and "NON_NULL", used to determine whether to exclude attributes (fields).

Syntax

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

Example

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;;
   }
}

Output

{
   "empId" : 115
}

The above is the detailed content of What is the importance of Jackson @JsonInclude annotation in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete