Home > Java > javaTutorial > body text

How to ignore a certain class during serialization using Jackson in Java?

WBOY
Release: 2023-08-19 09:25:04
forward
847 people have browsed it

How to ignore a certain class during serialization using Jackson in Java?

The Jackson @JsonIgnoreType Annotations can be used to ignore a class during the serialization process, and can mark all attributes or fields of a class Ignored when serializing and deserializing JSON objects.

Syntax

@Target(value={ANNOTATION_TYPE,TYPE})
@Retention(value=RUNTIME)
public @interface JsonIgnoreType
Copy after login

Example

import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
import java.io.*;
public class JsonIgnoreTypeTest {
   public static void main(String args[]) throws IOException {
      Employee emp = new Employee();
      ObjectMapper mapper = new ObjectMapper();
      String jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(emp);
      System.out.println(jsonString);
   }
}
// Employee class
class Employee {
<strong>   </strong>@JsonIgnoreType<strong>
</strong>   public static class Address {
      public String firstLine = null;
      public String secondLine= null;
      public String thirdLine = null;
      @Override
      public String toString() {
         return "Address{" +
                "firstLine=&#39;" + firstLine+ &#39;\&#39;&#39; +
                ", secondLine=&#39;" + secondLine+ &#39;\&#39;&#39; +
                ", thirdLine=&#39;" + thirdLine + &#39;\&#39;&#39; +
                &#39;}&#39;;
      }
   } // end of Address class
   public long empId = 115;
   public String empName = "Raja Ramesh";
   public Address empAddress = new Address();
  <strong> </strong>@Override
   public String toString() {
      return "Employee{" +
             "empId=" + empId +
             ", empName=&#39;" + empName + &#39;\&#39;&#39; +
             ", empAddress=" + empAddress +
             &#39;}&#39;;
   }
}
Copy after login

Output

{
   "empId" : 115,
   "empName" : "Raja Ramesh"
}
Copy after login

The above is the detailed content of How to ignore a certain class during serialization using Jackson in Java?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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!