java.lang.ClassNotFoundException: javax.annotation.PostConstruct

When upgrading to the latest tools and libraries in a Spring Boot application with OpenAPI code generator in place the OpenAPI Kotlin generator introduced jakarta.annotation:jakarta.annotation-api. But with this in place the application refused to start with something like

org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat

Caused by: java.lang.IllegalStateException: StandardEngine[Tomcat].StandardHost[localhost].TomcatEmbeddedContext[] failed to start

Digging a bit deeper I found the real reason:

java.lang.NoClassDefFoundError: javax/annotation/PostConstruct

This can be fixed by also including javax.annotation:javax.annotation-api as a dependency. The new jakarta based library only includes jakarta.annotation.* but not javax.annotation.*. But Spring Boot still requires the old namespace to work. I didn’t dig deeper why including the dependency broke the app though it was working before without both of the dependencies.

Another great sample for how convenient this rename is and further will be.