यदि Java ऑब्जेक्ट्स में ऐसे फील्ड्स हैं जिन्हें सीरियलाइज़ नहीं करना चाहते, तो हम Jackson लाइब्रेरी में @JsonIgnore एनोटेशन का उपयोग कर सकते हैं। @JsonIgnore का उपयोग फील्ड लेवल पर किया जा सकता है, सीरियलाइज़ेशन और डीसीरियलाइज़ेशन के दौरान फील्ड्स को इग्नोर करने के लिए।
सिंटैक्स
public @interface JsonIgnore
उदाहरण
import java.io.*;
import java.util.*;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.annotation.*;
public class JsonIgnoreAnnotationTest {
public static void main(String args[]) throws JsonGenerationException, JsonMappingException, IOException {
Employee emp = new Employee();
emp.setFirstName("Raja");
emp.setLastName("Ramesh");
emp.setEmpId(120);
emp.getTechnologies().add("Java");
emp.getTechnologies().add("Scala");
emp.getTechnologies().add("Python");
ObjectMapper mapper = new ObjectMapper();
mapper.writerWithDefaultPrettyPrinter().writeValue(System.out, emp);
}
}
// Employee क्लास
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"firstName",
"lastName",
"technologies",
"empId" })
class Employee {
@JsonProperty("EMPLOYEE_ID")
private int empId;
@JsonProperty("EMPLOYEE_FIRST_NAME")
private String firstName;
@JsonProperty("EMPLOYEE_LAST_NAME")
private String lastName;
@JsonIgnore
private List<String> technologies = new ArrayList<>();
public int getEmpId() { return empId; }
public void setEmpId(int empId) { this.empId = empId; }
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; }
public List<String> getTechnologies() { return technologies; }
public void setTechnologies(List<String> technologies) { this.technologies = technologies; }
}
आउटपुट
{
"EMPLOYEE_FIRST_NAME" : "Raja",
"EMPLOYEE_LAST_NAME" : "Ramesh",
"EMPLOYEE_ID" : 120
}