How to specify jackson to only use fields - preferably globally
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Jackson is a popular Java library used to convert Java objects to JSON and vice versa. By default, Jackson uses both fields and getter/setter methods when serializing/deserializing objects. However, there might be scenarios where you want Jackson to serialize/deserialize only the fields defined in the Java object, ignoring any getter or setter methods. This guide explains how to globally configure Jackson to use fields only, along with technical insights and practical examples.
Why Use Fields Only?
Using fields only can simplify serialization/deserialization processes by ignoring any additional logic encapsulated in getters/setters. This is especially useful in:
- Data Transfer Objects (DTOs): When the objects are merely data carriers with no behavior.
- Simplifying Serialization: To maintain a flat structure and avoid potential side effects of getter methods.
- Consistency: Ensuring consistency in JSON structure without being affected by additional accessor logic.
Configuring Jackson to Use Fields Only
Jackson configuration can be done in several ways: through annotations, by customizing ObjectMapper, or by implementing a custom serialization strategy. We will focus on configuring ObjectMapper to achieve this globally.
Jackson Annotations
Jackson provides annotations like @JsonIgnore, @JsonProperty, etc., to customize serialization/deserialization at a granular level. However, if you aim for a broad application where Jackson uses fields exclusively, modifying ObjectMapper is a suitable approach.
Configuring ObjectMapper
To globally configure Jackson to use fields only, you need to adjust the default access type. Here is a practical implementation:
In this example, objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE) disables visibility for all access types (fields, getters, setters, etc.) first. Then, objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY) enables visibility specifically for fields, allowing Jackson to serialize and deserialize fields directly.
Key Considerations
- Limited Flexibility: While using fields only simplifies the model, it limits your control over JSON structure customization provided by getters and setters.
- Immutable Objects: For objects preferring immutability, consider using constructors for deserialization.
- Annotations Overriding: Global settings can be overridden using Jackson annotations on specific fields or methods, offering a balance between global and local configurations.
Summary Table
| Aspect | Details |
| Default Behavior | Uses both fields and getters/setters |
| Fields Only Configuration | Customize ObjectMapper with PropertyAccessor.FIELD visibility |
| Advantages | Simplicity, consistency, and avoidance of side effects |
| Disadvantages | Limits flexibility and customization options available via accessors |
| Use Cases | Ideal for DTOs, strict serialization policies |
Additional Details and Enhancements
Potential Issues and Solutions
- Performance Concerns: Direct field access can be faster, but ensure that reflection visibility checks do not become a bottleneck.
- Security Implications: Make sure sensitive fields that shouldn’t be serialized are properly annotated with
@JsonIgnore.
Testing and Validation
Always test the serialization/deserialization process to ensure that the JSON output matches expectations. Unit tests leveraging JSON Schema validation can guarantee compliance with API contracts.
Adopting a fields-only approach with Jackson can lead to cleaner, more predictable JSON management, especially when working with data-centric applications, where data integrity and simplicity are paramount. However, be prepared for trade-offs, particularly around flexibility and the injection of business logic through getters/setters.
Related reading
- How to specify Java version in Spring/Spring Boot pom.xml?
- How to specify packagesToScan in HibernateJpaAutoConfiguration?
- How to specify prefix for all controllers in Spring Boot?
- How to specify the JDK version in Android Studio?
- How to split a delimited string to a ListString
- How to split a String by space
- How to start @KafkaListener based on start flag
- How to start new activity on button click
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.