Home > Java > javaTutorial > body text

How to ignore null and void fields using Jackson library in Java?

WBOY
Release: 2023-08-30 13:17:05
forward
805 people have browsed it

How to ignore null and void fields using Jackson library in Java?

Jackson is a library for Java that has very powerful data binding capabilities and provides a framework to serialize custom Java objects into JSON, and deserialize the JSON back into a Java object. The Jackson library provides the @JsonInclude annotation, which can control the serialization of the entire class or its individual fields during serialization based on the value.

@JsonInclude annotation contains the following two values ​​

  • Include.NON_NULL: Indicates that only attributes with non-null values ​​are included in JSON.
  • Include.NON_EMPTY: Indicates that only attributes that are not empty are included in JSON.

Example

import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
public class IgnoreNullAndEmptyFieldTest {
   public static void main(String[] args) throws JsonProcessingException {
      ObjectMapper mapper = new ObjectMapper();
      mapper.enable(SerializationFeature.INDENT_OUTPUT);
      Employee employee = new Employee(115, null, ""); // passing null and empty fields
      String result = mapper.writeValueAsString(employee);
      System.out.println(result);
   }
}
// Employee class
class Employee {
   private int id;
   @JsonInclude(Include.NON_NULL)
   private String firstName;
   @JsonInclude(Include.NON_EMPTY)<strong>
</strong>   private String lastName;
   public Employee(int id, String firstName, String lastName) {
      super();
      this.id = id;
      this.firstName = firstName;
      this.lastName = lastName;
   }
   public int getId() {
      return id;
   }
   public void setId(int id) {
      this.id = id;
   }
   public String getFirstName() {
      return firstName;
   }
   public void setFirstName(String firstName) {
      this.firstName = firstName;
   }
   public String getLastName() {
   return lastName;
   }
   public void setLastName(String lastName) {
      this.lastName = lastName;
   }
}
Copy after login

Output

<strong>{
 "id" : 115
}</strong>
Copy after login

The above is the detailed content of How to ignore null and void fields using Jackson library 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!