A Complete Guide to Migrating to Spring Boot 4.0.1

A Complete Guide to Migrating to Spring Boot 4.0.1

Technical Guide: Upgrading to Spring Boot 4.0.1

This guide outlines the technical requirements, dependency changes, and configuration updates necessary to migrate a Spring Boot application to version 4.0.1.

1. Build Environment Update
Before updating application dependencies, ensure your build tool is compatible with Spring Boot 4.0.1.
Gradle Version: Update to Gradle 9.2.1. This version is required to support the updated Spring Boot plugins and dependencies.

2. Dependency Management
Update your build.gradle.kts dependencies to their compatible versions.
Spring Boot Starters: Ensure all standard starters are updated to the managed versions provided by the Spring Boot 4.0.1 BOM:

  • spring-boot-starter-actuator
  • spring-boot-starter-data-mongodb-reactive
  • spring-boot-starter-security
  • spring-boot-starter-webflux
  • spring-boot-starter-oauth2-client

Third-Party Library Updates: You must manually update several key libraries to versions compatible with Spring Boot 4:

  • Redisson: Update redisson-spring-boot-starter to 4.1.0.
  • SpringDoc OpenAPI (Swagger): Update to springdoc-openapi-starter-webflux-ui:3.0.1 to maintain API documentation support.

3. Breaking Change: Jackson 3 Migration
Spring Boot 4.0.1 moves from Jackson 2 to Jackson 3, which introduces significant namespace changes. This will cause compilation errors until references are updated.

Dependency Changes: Replace the standard Kotlin module dependency:

// Remove or Update
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
// Add New Tools Module
implementation("tools.jackson.module:jackson-module-kotlin")
// Maintain backward compatibility if needed for specific libs
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.20.1")

Code Refactoring Required:

  • Imports: Update your import statements for the object mapper.
    From: com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
    To: tools.jackson.module.kotlin.jacksonObjectMapper

  • Codecs: If you are using Redisson or custom codecs, update TypedJsonJacksonCodec to TypedJsonJackson3Codec.

4. Configuration Properties Updates
Several properties in application.properties have been renamed or require explicit enablement.
MongoDB Configuration: The spring.data.mongodb prefix has been simplified. You must remove the .data segment from your keys.

Full Mapping:

spring.mongodb.host=192.168.1.122
spring.mongodb.port=27017
spring.mongodb.database=AppDBTest
spring.mongodb.username=admin
spring.mongodb.password=secret
spring.mongodb.authentication-database=admin

Swagger UI: SpringDoc now requires explicit properties to enable the UI and API docs endpoints:

springdoc.api-docs.enabled=true
springdoc.swagger-ui.enabled=true

5. Tooling: Replacing Ktlint with Spotless
Older versions of ktlint may not be supported in this environment. It is recommended to switch to the Spotless Gradle plugin for code formatting.

Implementation: Add the plugin to your build.gradle.kts:

plugins {
    id("com.diffplug.spotless") version "8.1.0"
}

Configuration: Configure Spotless to handle both Kotlin and Java formatting:

spotless {
  kotlin {
    target("**/*.kt")
    ktfmt("0.53")
    trimTrailingWhitespace()
    endWithNewline()
  }

  java {
    target("**/*.java")
    googleJavaFormat("1.19.2")
    removeUnusedImports()
    trimTrailingWhitespace()
    endWithNewline()
  }

  // Bind to compilation tasks
  tasks.named("spotlessCheck") { dependsOn("compileKotlin", "compileJava") }
  tasks.named("spotlessApply") { dependsOn("compileKotlin", "compileJava") }
}

Read more