ObjectMapper can't deserialize without default constructor after upgrade to Spring Boot 2
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
With the upgrade to Spring Boot 2, developers might encounter a common issue involving Jackson's ObjectMapper — specifically, its inability to deserialize certain objects without a default constructor. This predicament is partly due to changes in how Spring Boot configures the Jackson library and how Jackson itself has evolved over recent versions. This article delves into why this issue arises, how it affects Kotlin and Java developers differently, and presents effective solutions.
Understanding Jackson and ObjectMapper
Jackson is a popular library for processing JSON data in Java applications. The ObjectMapper class is a central part of this library, responsible for converting between JSON and Java objects. A typical issue that arises with Jackson involves class deserialization when there is no default (no-argument) constructor available.
Why Does Jackson Require a Default Constructor?
Jackson traditionally requires a default constructor to instantiate an object before populating its fields during deserialization. In environments where classes must be immutable or where fields are initialized only through a constructor, this poses a problem because such classes do not naturally include a no-argument constructor.
Spring Boot 2: Changes and Impacts
Default Configuration Changes
Spring Boot 2 ships with an updated set of default configurations for its components, Jackson included. It automatically registers Jackson's ParameterNamesModule if Kotlin support is enabled, which allows the library to infer constructor parameters using reflection. However, Java developers don't have this luxury by default.
Kotlin Versus Java
- Kotlin: Kotlin classes necessitate the use of primary constructors, often without additional constructor overloading. Default parameters in Kotlin make it easier to define flexible constructors.
- Java: Java developers traditionally rely on overloading and explicit constructor definitions, particularly for immutable data structures.
Key Issue: Missing Default Constructor
In Spring Boot 2, developers may find that classes without a default constructor fail during JSON deserialization with Jackson. This scenario often produces an error message akin to: "Cannot construct instance of [class] (no Creators, like default constructor, exist)."
Common Solutions
To navigate this issue, developers have several solutions at their disposal:
- Add a Default Constructor: The most straightforward solution, albeit not always feasible, involves adding a default constructor to the class.
- Use Annotations: Jackson provides annotations such as
@JsonCreatorto specify a constructor or factory method for instantiation, as demonstrated below:
- Enable "Fail on Null for Primitive" Configuration: Enabling this configuration option can prevent Jackson from skipping constructor arguments when their value is
null.
Configuration Using Spring Boot
For projects using Spring Boot, you can customize Jackson's behavior globally by adding configurations in the application.properties or application.yml:
Alternatively, you might define a @Bean configuration for ObjectMapper in a @Configuration class:
Conclusion
The transition to Spring Boot 2 highlights the evolving requirements for effective JSON deserialization within Java applications. By understanding the necessity and alternatives to default constructors, developers can enhance their persistence layer's robustness without compromising their data structures' immutability or design purity.
Here's a quick summary table of key points:
| Key Changes | Description |
| Jackson Support | Added support for more flexible constructor configurations. |
| Default Constructor Requirement | Spring Boot 2 sometimes enforces the need for default constructors in Java. |
| Annotations | Use @JsonCreator and @JsonProperty to specify constructors when needed. |
| Configurations | Adjust ObjectMapper configurations for automatic handling of nulls. |
| Spring Boot Configuration | Customize ObjectMapper globally via application.yml or through a @Bean. |
By leveraging these insights and strategies, developers can efficiently address deserialization challenges in Spring Boot 2 applications and ensure smooth system operations.

