Intro to SpringBoot3 with Kotlin: Part 1 — Initialize the Application

Daniel Peach
5 min readOct 5, 2023

--

The Series

The Source Code

Introduction & Prerequisites

In this series, we will be discussing how to create a REST API with SpringBoot, written in Kotlin, with Gradle (with kotlin syntax) as the build system. We expect a basic understanding of a few things.

  • An understanding of what a web server is, and what controllers, services, and repos typically represent.
  • A basic grasp of Kotlin or Java syntax, as well as a general understanding of programing paradigms in general

IntelliJ is the recommended IDE for working with SpringBoot and Kotlin.

What is SpringBoot?

SpringBoot is an opinionated way to write Spring applications.

The Spring framework itself provides us the plumbing of the infrastructure that we need to build any kind of application, and the Spring ecosystem comes with a huge array of libraries that integrate with it. However, the Spring Framework and its ecosystem is so large and flexible, it can at times be tedious, or overwhelming even, to configure and keep up to date a project with it.

This is where SpringBoot comes in, providing us with opinionated and managed sets of dependencies and configurations.

spring.start.io

SpringBoot has a related tool called the Spring Initializer, at https://spring.start.io, that can help us spin up a new project quite easily.

Instead of searching the web for articles or example starting POMs or Gradle scripts, we can tell the Spring Initializer what our project is and what dependencies we want to start with and it will give us the scaffolded project for us.

It does this by asking for information like the programming language, build system, and dependencies we wish to use. In fact, it provides a list of “starters”, which are sets of version-managed dependencies that are meant to work well together to perform a particular goal, such as run a web server.

You can also use the Spring Initializer built into IntelliJ, which we will do and recommend you do as well if you can.

That being said, let’s use this tool to start our project.

Setup

Load your IDE and start a new project using the built in initializer, or go to spring.start.io. Give your project a name (we will call our server springboot3-kotlin), select Kotlin as the language and Gradle as the build system, and hit “Next”.

We will select the following dependencies (aka starters):

  • Spring Boot Devtools, to give us tooling like preset configuration values and live reload capabilities
  • Spring Web, to give us a web server and needed tooling out of the box
  • Spring Data JPA, to give us best-in-class database and entity management
  • H2 Database, to give us an in-memory database

More on all these later, and we can always add more dependencies down the road, such as a real MySQL or Postgres database, testing libraries, and more.

Click “Create” (or “Generate” if using the web tool), and open (or download, unzip, and open) in IntelliJ. Let the IDE index the project, and take a look at the build.gradle.kts it created for us.

The Gradle Buildfile

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
id("org.springframework.boot") version "3.1.4"
id("io.spring.dependency-management") version "1.1.3"
kotlin("jvm") version "1.8.22"
kotlin("plugin.spring") version "1.8.22"
kotlin("plugin.jpa") version "1.8.22"
}

group = "com.example"
version = "0.0.1-SNAPSHOT"

java {
sourceCompatibility = JavaVersion.VERSION_17
}

repositories {
mavenCentral()
}

dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
developmentOnly("org.springframework.boot:spring-boot-devtools")
runtimeOnly("com.h2database:h2")
testImplementation("org.springframework.boot:spring-boot-starter-test")
}

tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs += "-Xjsr305=strict"
jvmTarget = "17"
}
}

tasks.withType<Test> {
useJUnitPlatform()
}

Under plugins, notice

id("org.springframework.boot") version "3.1.4"
id("io.spring.dependency-management") version "1.1.3"

Notice that in dependencies, we don’t specify any versions for the supported spring-boot starters. This is because the plugins above manage these for us. A security exploit is found in one of your dependencies? The Spring team will be alerted and will publish a patch. Update your SpringBoot version, and all downstream dependencies will be updated as needed.

Next, notice the plugins

kotlin("plugin.spring") version "1.8.22"
kotlin("plugin.jpa") version "1.8.22"

In many Kotlin projects, it is desirable to create no-arg constructors and open classes for certain parts of the application. For this, Kotlin provides the no-arg and all-open compiler plugins. This is such a common need for a given set of spring and jpa classes, that they have pre-configured versions of the plugins that we see here.

The rest of the Gradle file specifies project details and the dependencies we selected in the initializer.

Run the Appliation

Note: I tend to refactor the the main Application class to be called simply Application. This is personal preference, but if you would like to do so, simply go to the Application class, highlight the name, left click, and navigate to Refactor > Rename > “Application” > Enter > Select All > OK. This will rename your class, the file, and do the same for the relevant tests.

To run the application, you can either run ./gradlew :bootRun (or the corresponding Windows command), or preferably, you can go to the Application class and hit the “run” icon the left gutter.

Notice on the Application class, we have the @SpringBootApplication annotation. This annotation takes a look at our application and the dependencies we have on the classpath and figures out how to start up and run our application. In this case we have added Spring Web, so it knows to configure and run a web server.

In the main method of the application class, we call Spring’s runApplication method, which is what hands control of the application over to Spring, and from there we are able to inject our own code and business logic into the running of the application, which we will do soon.

Summary

Congratulations, you have created and ran a Spring Boot Application with Gradle and Kotlin.

In Part 2, we will create our first controller to see our web application in action.

--

--