After upgrading the services from Spring Boot 2.7 to 3.2.5 without major issues I was not expecting an issue when upgrading from 3.2.5 to 3.3.1 as it is a minor upgrade and in theory we should have (many) breaking changes. Still, unfortunately, that was not true and we had a serialization issue.

The message below shows the stack trace with the message.

Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException:
 Java 8 optional type java.util.Optional<xyz.joaovieira.models.Stock> not supported by default: 
 add Module "com.fasterxml.jackson.datatype:jackson-datatype-jdk8" to enable handling (through reference chain: ….

As it turns out in the new version of Spring Boot 3.3.1 the Jackson databing library was upgraded from 2.15.4 to 2.17.1 (in Spring Boot 3.3.0) and between these two versions, one of the bugs or improvements was added related to the optional class. This is the snippet of the code with the change from this issue and this code change.

Changes in Jackson Databind library

Changes in Jackson Databind library

I’m not very smart so this doesn’t mean very much to me, but the side effect of this for our project was that we started to get the following error message when trying to serialize an object that had an Optional get method on it.

As the message clearly indicates, we need to add the JDK8 module to our object mapper for us to be able to serialize the optional.

objectMapper.registerModule(new Jdk8Module());;

And doing that really fixes the problem. In a way, it was our mistake that we didn’t have this JDK eight module already in the objects mapper but for some reason, it was working before and we did change it. It stopped working.

So yeah, if you get near like the one shown above you just need to add the JDK8Module to your object mapper as the messages say, and the reason is because of that code change in the Jackson Databind library.

Cheers.