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

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

WBOY
Release: 2023-08-19 22:01:13
forward
899 people have browsed it

在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>
Copy after login

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;;
   }
}
Copy after login

Output

{
   "empId" : 115
}
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template