Skip to content

Lesson 5 of 9 · 25m

Spring Boot with Kotlin: Where the Magic Still Lives

Configure Spring Boot for Kotlin correctly, understand proxy and reflection friction, and keep framework conveniences from erasing explicit design.

Spring Boot and Kotlin work well together, but they do not erase each other's assumptions. Spring grew up around JavaBeans, classpath scanning, proxies, and mutable configuration. Kotlin grew up around nullability, immutability, final classes, and explicit constructors. Production services sit exactly where those worldviews rub.

Start with the correct plugins and dependencies

plugins {
    kotlin("jvm") version "2.1.21"
    kotlin("plugin.spring") version "2.1.21"
    id("org.springframework.boot") version "3.4.5"
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor")
}

The Spring Kotlin plugin opens classes that Spring proxies. Without it, final classes and methods can produce confusing startup failures. Jackson's Kotlin module is not optional if you use data classes in APIs.

Constructor injection matches Kotlin's strengths

@Service
class RefundService(
    private val payments: PaymentGateway,
    private val ledger: LedgerRepository,
) {
    suspend fun refund(command: RefundCommand): Receipt =
        withContext(Dispatchers.IO) {
            val receipt = payments.refund(command)
            ledger.record(command, receipt)
            receipt
        }
}

Constructor injection makes dependencies visible, final, and testable. Field injection with lateinit var exists to satisfy frameworks, not because it models your service well.

Coroutines need reactor awareness

Spring WebFlux supports suspend controller methods, but the execution model is reactive. Blocking JDBC, ThreadLocal-heavy security utilities, or synchronous clients can still pin scarce event-loop threads:

@RestController
class RefundController(private val refunds: RefundService) {
    @PostMapping("/v1/refunds")
    suspend fun create(@RequestBody request: RefundRequest): ReceiptResponse =
        refunds.refund(request.toCommand()).toResponse()
}

A suspend controller method does not automatically make the repository non-blocking. Follow the whole path. If the stack is blocking, Spring MVC with coroutine adapters may be more honest than pretending you built a reactive service.

Field notes

  • Data classes and Jackson work well, but default constructor values only behave correctly with jackson-module-kotlin registered.
  • lateinit in tests is sometimes pragmatic. In production types it usually signals framework leakage into the domain.
  • WebFlux, MVC, JDBC, R2DBC, Reactor, and coroutines are not interchangeable. Choose the execution model per dependency honestly.
  • Spring Boot reduces boilerplate, not the need to know which proxy, thread pool, serializer, and transaction manager are active.

Was this lesson helpful?

Sign in to rate