CodexBloom - Programming Q&A Platform

Java 17 - implementing JSON Serialization of Custom Objects Using Jackson After Upgrading

👀 Views: 198 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-08
jackson json java-17 Java

I'm having trouble with I'm relatively new to this, so bear with me. After upgrading my application to Java 17, I'm working with unexpected behavior when trying to serialize a custom object to JSON using the Jackson library (version 2.12.5). The object contains a field of type `List<AnotherCustomObject>`, which itself has a field of type `LocalDate`. When I attempt to serialize my main object, I receive the following behavior: ``` com.fasterxml.jackson.databind.exc.InvalidDefinitionException: want to construct instance of `AnotherCustomObject` (no Creators, like default constructor, exist) ``` I have verified that `AnotherCustomObject` has a public no-argument constructor, and I am using the `@JsonCreator` annotation on the constructor that takes parameters. Below is the relevant part of my code: ```java import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.ObjectMapper; import java.time.LocalDate; import java.util.List; public class MainObject { private List<AnotherCustomObject> items; public MainObject(List<AnotherCustomObject> items) { this.items = items; } } class AnotherCustomObject { private LocalDate date; @JsonCreator public AnotherCustomObject(@JsonProperty("date") LocalDate date) { this.date = date; } } public class JsonTest { public static void main(String[] args) throws Exception { ObjectMapper mapper = new ObjectMapper(); AnotherCustomObject item = new AnotherCustomObject(LocalDate.now()); MainObject mainObject = new MainObject(List.of(item)); String jsonString = mapper.writeValueAsString(mainObject); System.out.println(jsonString); } } ``` I've tried adding the `@JsonConstructor` annotation to the constructor of `AnotherCustomObject`, but that didn't resolve the scenario. What could be causing this serialization question after the Java version upgrade? Is there a new requirement in Jackson or Java 17 that I might be missing? For context: I'm using Java on Windows. What am I doing wrong? For context: I'm using Java on Linux. What am I doing wrong? I'm developing on Debian with Java. What's the best practice here?