Mastering Jackson: 5 Ways to Exclude Fields in Spring Boot JSON
This article explains five practical techniques for ignoring specific fields during JSON serialization and deserialization in Spring Boot using Jackson, covering class‑level, field‑level, type‑level, filter‑based, @JsonView, and custom serializer approaches with complete code examples and result screenshots.
Introduction
In Spring Boot development, JSON is the primary data exchange format between front‑end requests and back‑end responses. Jackson, the default JSON processor, offers powerful features for converting between objects and JSON. However, not all object fields should be serialized or deserialized, especially sensitive data such as passwords.
This article presents five practical methods to ignore JSON fields using Jackson.
1. Overview
Jackson provides several annotations and configurations to control field inclusion during JSON conversion. The following sections demonstrate each method with code snippets and expected output.
2. Practical Cases
2.1 Class‑Level Field Ignoring
Use @JsonIgnoreProperties on a class to specify fields that should be omitted.
@JsonIgnoreProperties(value = {"password", "idNo"})
public class User {
private Long id;
private String name;
private String password;
private String idNo;
private String address;
}Controller example:
@GetMapping("/query")
public User query() {
return new User(1L, "Spring Boot实战案例200讲", "1234567890", "1828381828225677", "四川乌鲁木齐");
}Result: the password and idNo fields are omitted.
2.2 Field‑Level Ignoring
Apply @JsonIgnore directly on the fields that should be excluded.
public class User {
private Long id;
private String name;
@JsonIgnore
private String password;
@JsonIgnore
private String idNo;
private String address;
}The same endpoint now returns JSON without the annotated fields.
2.3 Ignoring All Fields of a Type
Annotate a class with @JsonIgnoreType to skip all its fields during serialization.
@JsonIgnoreType
public class Score {
private Long userId;
private double score;
}Include Score in User and the entire Score object will be ignored in the JSON output.
2.4 Using a Filter to Exclude Fields
Define a custom PropertyFilter and register it via Jackson2ObjectMapperBuilderCustomizer.
@Component
public class JsonFilterConfig implements Jackson2ObjectMapperBuilderCustomizer {
@Override
public void customize(Jackson2ObjectMapperBuilder builder) {
SimpleBeanPropertyFilter theFilter = SimpleBeanPropertyFilter.serializeAllExcept("password", "idNo");
FilterProvider filters = new SimpleFilterProvider().addFilter("userFilter", theFilter);
builder.filters(filters);
}
}Apply the filter to the class:
@JsonFilter("userFilter")
public class User {
private Long id;
private String name;
private String password;
private String idNo;
private String address;
private Score score;
}2.5 @JsonView Filtering
The @JsonView annotation allows selective serialization based on view classes, enabling different field sets for public or internal APIs. (Detailed usage is linked in the original article.)
2.6 Custom Serializer
Implement a custom serializer by extending JsonSerializer<T> and overriding serialize to manually write desired fields.
public class UserSerializer extends JsonSerializer<User> {
@Override
public void serialize(User user, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeStartObject();
gen.writeNumberField("id", user.getId());
gen.writeStringField("name", user.getName());
gen.writeStringField("address", user.getAddress());
gen.writeEndObject();
}
}Annotate the class:
@JsonSerialize(using = UserSerializer.class)
public class User {
// fields ...
}Result Screenshots
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
