In Jackson, we can use @JsonIgnore
to ignore a single field, @JsonIgnoreProperties
to ignore multiple fields and @JsonIgnoreType
to ignore a specified type during JSON serialization and deserialization.
Table of contents:
- 1. Setup Jackson
- 2. Jackson @JsonIgnore – Ignores a field
- 3. Jackson @JsonIgnoreProperties – Ignores multiple fields
- 4. Jackson @JsonIgnoreType – Ignores a specified type
- 5. Download Source Code
- 6. References
P.S Tested with Jackson 2.17.0
1. Setup Jackson
Puts jackson-databind
at pom.xml
.
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.17.0</version>
</dependency>
2. Jackson @JsonIgnore – Ignores a field
The annotation @JsonIgnore
is used directly on the field or getter/setter to ignore it during JSON serialization and deserialization.
package com.mkyong.json.model;
import com.fasterxml.jackson.annotation.JsonIgnore;
public class User {
private String name;
@JsonIgnore
private int age;
// getters, setters, constructors and etc
}
Testing ignoring fields with Jackson.
package com.mkyong.json.jackson.tips;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mkyong.json.model.User;
public class IgnoreFieldExample {
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
User user = new User();
user.setName("mkyong");
user.setAge(42);
try {
String jsonOutput = mapper.writeValueAsString(user);
System.out.println(jsonOutput);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
}
output
{"name":"mkyong"}
3. Jackson @JsonIgnoreProperties – Ignores multiple fields
The annotation @JsonIgnoreProperties
is used at the class level to ignore multiple fields by name.
package com.mkyong.json.model;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(value = {"name", "age"})
public class User {
private String name;
private int age;
// getters, setters, constructors and etc
}
Output
{}
4. Jackson @JsonIgnoreType – Ignores a specified type
The annotation @JsonIgnoreType
is used on a class definition to automatically ignore all fields of this class.
package com.mkyong.json.model;
import com.fasterxml.jackson.annotation.JsonIgnoreType;
@JsonIgnoreType
public class SecretData {
private String password;
private String type;
// getters, setters, constructors and etc
}
package com.mkyong.json.model;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
public class User {
private String name;
private int age;
// this class with ignore automatically
private SecretData secret;
// getters, setters, constructors and etc
}
Testing ignoring a specified type with Jackson.
package com.mkyong.json.jackson.tips;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mkyong.json.model.SecretData;
import com.mkyong.json.model.User;
public class IgnoreFieldExample {
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
User user = new User();
user.setName("mkyong");
user.setAge(42);
user.setSecret(new SecretData("123", "sha256"));
try {
String jsonOutput = mapper.writeValueAsString(user);
System.out.println(jsonOutput);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
}
output
{"name":"mkyong","age":42}
5. Download Source Code
$ git clone https://github.com/mkyong/java-json
6. References
- Jackson Github
- Jackson data-binding
- Jackson Convert Java Object to / from JSON
- How to parse JSON string with Jackson
- How to ignore null fields with Jackson
The post How to ignore a field with Jackson appeared first on Mkyong.com.