In this tutorial we will learn how to create a Spring Boot application based on the Kotlin programming language.

Kotlin is a statically typed programming language that runs on the Java virtual machine and also can be compiled to JavaScript source code or use the LLVM compiler infrastructure. It is sponsored and developed by JetBrains.

For the JVM, it compiles to Java bytecode and it’s completely interoperable with Java. This means you can write Kotlin and Java in the same project and they’ll work hand in hand. Intellij (Jetbrains IDE for Java) even allows direct conversion of Java code to Kotlin.

For a lot of developers, it’s become a language that remedies a lot of problems that have plagued Java for a while. Since Java was built a long time ago, it lacks some built in language features (Which languages like C# have) that make Asynchronous development easy. It’s not the most concise language either.

One of the key goals of Kotlin is to be interoperable with Java so that you can use Java and Kotlin together in the same project.
Here’s how you write a simple Hello World program in Kotlin:

package hello
fun main(args: Array<String>){
    println("Hello World");
}

In Kotlin, there are no static methods like in Java, so you can write those methods that you want to be
static as top-level functions.

Kotlin and Spring Boot

In this tutorial we will learn how to run a simple Kotlin class as part of Spring Boot environment using IntelliJ Idea as environment.

Start by creating a bare-bones Spring Boot project:

% spring init demo-kotlin

Now import the project from IntelliJ Idea and add a new kotlin folder under the src/main folder and src/test.

Next, create a new Kotlin class named SpringBootKotlinApp under the src/main/kotlin folder:

package com.example.demokotlin;

import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication

@SpringBootApplication

class SpringBootKotlinApp
fun main(args: Array<String>)
{
    SpringApplication.run(SpringBootKotlinApp::class.java, *args)
    print("Enter text: ")

    val stringInput = readLine()!!
    println("You entered: $stringInput")

}

This is a simple Kotlin class which reads a String from the command line and type the String as output.

Here is an overview of the Project from IntelliJ Idea:

spring boot kotlin

In order to compile and execute the above example, we need to add two things:

  • Kotlin dependencies to the project
  • Kotlin maven plugin
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.1.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>demo-kotlin</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>demo</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
		<kotlin.version>1.2.71</kotlin.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.jetbrains.kotlin</groupId>
			<artifactId>kotlin-stdlib-jre8</artifactId>
			<version>${kotlin.version}</version>
		</dependency>
		<dependency>
			<groupId>org.jetbrains.kotlin</groupId>
			<artifactId>kotlin-reflect</artifactId>
			<version>${kotlin.version}</version>
		</dependency>
	</dependencies>

	<build>
		<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
		<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>

			<plugin>
				<artifactId>kotlin-maven-plugin</artifactId>
				<groupId>org.jetbrains.kotlin</groupId>
				<version>${kotlin.version}</version>
				<configuration>
					<compilerPlugins>
						<plugin>spring</plugin>
					</compilerPlugins>
					<jvmTarget>1.8</jvmTarget>
				</configuration>
				<executions>
					<execution>
						<id>compile</id>
						<phase>compile</phase>
						<goals>
							<goal>compile</goal>
						</goals>
					</execution>
					<execution>
						<id>test-compile</id>
						<phase>test-compile</phase>
						<goals>
							<goal>test-compile</goal>
						</goals>
					</execution>
				</executions>
				<dependencies>
					<dependency>
						<groupId>org.jetbrains.kotlin</groupId>
						<artifactId>kotlin-maven-allopen</artifactId>
						<version>${kotlin.version}</version>
					</dependency>
				</dependencies>
			</plugin>
		</plugins>
	</build>
</project>

You can run the example application with:

./mvnw spring-boot:run

The expected output is:

INFO 10985 --- [           main] c.e.demokotlin.SpringBootKotlinAppKt     : Starting SpringBootKotlinAppKt on new-host-2 with PID 10985 (/home/francesco/springboot/demo-kotlin/target/classes started by francesco in /home/francesco/springboot/demo-kotlin)
INFO 10985 --- [           main] c.e.demokotlin.SpringBootKotlinAppKt     : No active profile set, falling back to default profiles: default
INFO 10985 --- [           main] c.e.demokotlin.SpringBootKotlinAppKt     : Started SpringBootKotlinAppKt in 1.256 seconds (JVM running for 11.618)
Enter text: frank
You entered: frank

Congratulations! You have just run your first Spring Boot application with Kotlin!