Global Configuration in Jackson: Using Fields Only for JSON Serialization and Deserialization

Dec 02, 2025 · Programming · 12 views · 7.8

Keywords: Jackson | JSON Serialization | Global Configuration | Field Visibility | ObjectMapper

Abstract: This article provides an in-depth exploration of how to globally configure Jackson to use only fields rather than properties (getters/setters) for JSON serialization and deserialization. By analyzing the visibility configuration mechanism of ObjectMapper, it details two primary implementation approaches: chained configuration based on VisibilityChecker and batch settings using PropertyAccessor. The article also supplements with special handling for boolean-type getters and configuration examples in Spring Boot, offering comprehensive and practical technical solutions for developers.

Overview of Jackson Serialization Mechanism

Jackson, as a widely used JSON processing library in the Java ecosystem, employs a hybrid strategy for serialization and deserialization by default. Specifically, it considers both class fields and properties (i.e., getter and setter methods). While this design offers flexibility, it can lead to issues in certain scenarios. For instance, when developers intend to use fields as the sole data source for serialization, automatic property detection may result in unintended data exposure or inconsistent serialization behavior.

Global Configuration Method 1: Chained Configuration with VisibilityChecker

According to the best practice answer, fine-grained global configuration can be achieved through ObjectMapper's VisibilityChecker. The core code is as follows:

ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(mapper.getSerializationConfig().getDefaultVisibilityChecker()
                .withFieldVisibility(JsonAutoDetect.Visibility.ANY)
                .withGetterVisibility(JsonAutoDetect.Visibility.NONE)
                .withSetterVisibility(JsonAutoDetect.Visibility.NONE)
                .withCreatorVisibility(JsonAutoDetect.Visibility.NONE));

This code uses chained calls to methods such as withFieldVisibility() and withGetterVisibility() to explicitly set field visibility to ANY (fields with any access modifier are visible), while getter, setter, and creator visibility are set to NONE (completely invisible). This configuration ensures that Jackson reads only field values during serialization, completely ignoring property methods.

Global Configuration Method 2: Batch Settings with PropertyAccessor

Another more concise configuration approach utilizes the PropertyAccessor enumeration, applicable to Jackson 2.0 and later versions:

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;   

...

ObjectMapper mapper = new ObjectMapper();    
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);

Here, visibility for all property accessors is first set to NONE via PropertyAccessor.ALL, and then field visibility is individually set to ANY. This method offers cleaner code but requires attention to its coverage—it similarly disables all access methods other than fields.

Special Case: Handling Boolean-Type Getters

In practical applications, boolean-type getter methods (typically named isXxx()) require special attention. Even if GETTER visibility is configured as NONE using the above methods, Jackson may still automatically detect these methods. The solution is to explicitly configure IS_GETTER:

mapper.setVisibility(PropertyAccessor.IS_GETTER, JsonAutoDetect.Visibility.NONE);

Or in annotation-based configuration:

@JsonAutoDetect(isGetterVisibility = Visibility.NONE, ...)

Spring Boot Integration Configuration

In Spring Boot projects, declarative configuration can be performed via application.properties or application.yml files:

jackson:
  visibility.field: any
  visibility.getter: none
  visibility.setter: none
  visibility.is-getter: none

This configuration approach is equivalent to programmatic configuration but aligns better with Spring Boot's configuration philosophy, facilitating environment management and externalized configuration.

Configuration Strategy Comparison and Selection Recommendations

The two main configuration methods each have their advantages and disadvantages: VisibilityChecker chained configuration provides finer-grained control, allowing individual settings such as creator visibility; whereas PropertyAccessor batch settings are more concise and clear. In actual projects, if only basic field-only serialization is needed, the PropertyAccessor method is recommended; if more complex visibility rules are required, VisibilityChecker should be chosen.

Considerations and Best Practices

When implementing global field-only serialization configuration, several points should be noted: First, ensure all fields requiring serialization have appropriate access modifiers (at least package-private). Second, consider using the @JsonIgnore annotation to exclude specific fields. Finally, it is advisable to manage the configured ObjectMapper instance via singleton or dependency injection to avoid redundant configuration.

Conclusion

Jackson offers flexible and powerful global configuration mechanisms, enabling developers to precisely control serialization behavior. By appropriately configuring visibility rules, JSON processing can be ensured to be entirely field-based, thereby enhancing code consistency and maintainability. Whether through programmatic APIs or Spring Boot configuration, this goal can be effectively achieved.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.