Write a Spring Boot Application with Kotlin (Part 1)
Note: This blog post was reviewed using AI for factual correctness and clarity. All content was tested in my private homelab to ensure accuracy.
Spring Boot and Kotlin are a fantastic combo for building modern backend services. Spring Boot brings batteries-included microservice support, and Kotlin makes writing code feel clean, expressive, and (dare I say?) fun.
If you’re coming from a Java background or just curious about how Kotlin fits into the Spring ecosystem, you’re in the right place.
In this post, we’ll start from scratch and build a Spring Boot app using Kotlin, Gradle, and a bit of magic.
🏗️ Step 1: Set Up the Project
Let’s kick things off by creating a new Gradle project:
$ mkdir spring-boot-kotlin
$ cd spring-boot-kotlin
$ gradle init
The CLI will walk you through a few prompts. Choose the following options:
- Project type →
basic - Build script DSL →
Kotlin - Use new APIs? → just press
Enterto stick with the default - Project name? → leave it as
spring-boot-kotlin(or customize if you like)
You’ll get a nice starter structure like this:
.
├── build.gradle.kts
├── gradle/
│ └── wrapper/
├── gradlew
├── settings.gradle.kts
🧠 Step 2: Add Kotlin Support
Right now, this is a barebones Gradle project. Time to give it a brain — Kotlin support!
Open up build.gradle.kts and add the Kotlin plugin:
plugins {
kotlin("jvm") version "2.1.0"
}
We’ll also configure the toolchain to use Java 21, which is now the recommended LTS version (Java 17+ is still supported):
kotlin {
jvmToolchain(21)
}
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(21))
}
}
🌱 Step 3: Add Spring Boot Plugins
Now let’s teach our project how to speak Spring Boot.
Update the plugins block to include:
plugins {
kotlin("jvm") version "2.1.0"
id("org.jetbrains.kotlin.plugin.spring") version "2.1.0"
id("org.jetbrains.kotlin.plugin.jpa") version "2.1.0"
id("org.springframework.boot") version "3.3.3"
id("io.spring.dependency-management") version "1.1.6"
}
Here’s what these plugins do:
org.springframework.boot— the core Spring Boot pluginkotlin.plugin.spring— makes Kotlin classes open if they have Spring annotations (because by default, Kotlin classes arefinal, which Spring doesn’t like)kotlin.plugin.jpa— optional, but handy if you’re using JPA/Hibernate (it helps generate no-arg constructors)
🧩 Step 4: Add Dependencies
Let’s give our app some power-ups!
Add this to the dependencies block:
dependencies {
implementation(kotlin("stdlib-jdk8"))
implementation(kotlin("reflect"))
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-actuator")
testImplementation("org.springframework.boot:spring-boot-starter-test")
}
Notice we aren’t manually specifying versions — that’s thanks to Spring Boot’s BOM (Bill of Materials), which handles compatible versions for you.
This is already handled by the io.spring.dependency-management plugin we added above.
And remember to declare where to pull dependencies from:
repositories {
mavenCentral()
}
🚀 Step 5: Write Your First Spring Boot App
Time to actually write some code!
Create a new Kotlin file under src/main/kotlin/com/example/Application.kt:
package com.example
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
@SpringBootApplication
class Application
fun main(args: Array<String>) {
runApplication<Application>(*args)
}
The @SpringBootApplication annotation tells Spring Boot, “Hey, start here!” and the runApplication call boots
everything up.
Now run your app:
$ ./gradlew bootRun
Spring Boot will spin up a server on http://localhost:8080.
Right now, we haven’t added any controllers, so the root path will return a 404. But! You can still access the health
check:
http://localhost:8080/actuator/health
🎉 What’s Next?
That’s it for Part 1! You now have a Kotlin-based Spring Boot app running with Gradle.
In the next part, we’ll:
- Add a REST controller
- Return real responses instead of 404s
- Look at how Kotlin makes Spring code more expressive and concise
Until then — happy coding!