- Ignore Fields at the Class Level
- Ignore Field at the Field Level
- Ignore all Fields by Type
Dependencies
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
- jackson-annotations-2.9.8.jar
- jackson-core-2.9.8.jar
- jackson-databind-2.9.8.jar
1. Ignore Fields at the Class Level
package net.javaguides.jackson.ignore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(value = {
"id",
"firstName"
})
public class CustomerDTO {
private final String id;
private final String firstName;
private final String lastName;
public CustomerDTO(String id, String firstName, String lastName) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
public String getId() {
return id;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}
package net.javaguides.jackson.ignore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class IgnoreFieldTest {
public static void main(String[] args) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
CustomerDTO dtoObject = new CustomerDTO("CUST100", "Tony", "Stark");
String dtoAsString = mapper.writeValueAsString(dtoObject);
System.out.println(dtoAsString);
}
}
{"lastName":"Stark"}
2. Ignore Field at the Field Level
package net.javaguides.jackson.ignore;
import com.fasterxml.jackson.annotation.JsonIgnore;
public class CustomerDTO {
@JsonIgnore
private final String id;
@JsonIgnore
private final String firstName;
private final String lastName;
public CustomerDTO(String id, String firstName, String lastName) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
public String getId() {
return id;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}
package net.javaguides.jackson.ignore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class IgnoreFieldTest {
public static void main(String[] args) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
CustomerDTO dtoObject = new CustomerDTO("CUST100", "Tony", "Stark");
String dtoAsString = mapper.writeValueAsString(dtoObject);
System.out.println(dtoAsString);
}
}
{"lastName":"Stark"}
3. Ignore all Fields by Type
@JsonIgnoreType
public static class Name {
public String firstName;
public String lastName;
public Name(String firstName, String lastName) {
super();
this.firstName = firstName;
this.lastName = lastName;
}
}
package net.javaguides.jackson.ignore;
import com.fasterxml.jackson.annotation.JsonIgnoreType;
public class UserDTO {
public int id;
public Name name;
public UserDTO(int id, Name name) {
super();
this.id = id;
this.name = name;
}
@JsonIgnoreType
public static class Name {
public String firstName;
public String lastName;
public Name(String firstName, String lastName) {
super();
this.firstName = firstName;
this.lastName = lastName;
}
}
}
package net.javaguides.jackson.ignore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonIgnoreTypeTest {
public static void main(String[] args) throws JsonProcessingException {
UserDTO.Name name = new UserDTO.Name("John", "Doe");
UserDTO user = new UserDTO(1, name);
String result = new ObjectMapper()
.writeValueAsString(user);
System.out.println(result);
}
}
{"id":1}
Related Articles
- Change Field Name in JSON using Jackson (popular)
GitHub Repository
The source code of this article available on my GitHub repository at https://github.com/RameshMF/jackson-json-tutorialContinue Learning with Java Guides
Want to take your programming skills to the next level? Explore my practical Udemy courses on Java, Spring Boot, Microservices, Full-Stack Development, Docker, and Generative AI. You can also follow my structured learning roadmaps to become a professional developer.
By Ramesh Fadatare - Udemy Instructor Partner
Comments
Post a Comment
Leave Comment