Spring boot using Kotlin

In this tutorial i will show you how to getting started with Spring Boot and Kotlin. Kotlin has become more and more popular the last couple of years and deserves a tutorial on its own. 

Why use Kotlin instead of Java?
Kotlin has a few advantages over Java and that includes that it does not have as much boilerplate code as Java and operator overloading. That are only 2 of the features of Kotlin, you can read more about Kotlin here.

What you will need


First thing we need to do is to create a IntelliJ project. I chose a Gradle Kotlin/JVM project and named it HelloKotlinSpringBoot, you can find source code for the project here.

When you click finish IntelliJ will create the project for you and index all the necessary files.

The next step is modify the gradle file for the project to include the necessary dependencies to both Kotlin and Spring Boot. Modify the build.gradle file to look like this.

buildscript {
    ext.kotlin_version = '1.3.70'
    ext.spring_boot_version = '2.1.0.RELEASE'
    repositories {
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version"
        classpath "org.springframework.boot:spring-boot-gradle-plugin:$spring_boot_version"

apply plugin: 'kotlin'
apply plugin: "kotlin-spring"
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

jar {
    baseName = 'HelloKotlinSpringBoot'
    version = '1.0.0'

repositories {

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    compile "org.springframework.boot:spring-boot-starter-web"

This includes both Kotlin dependencies as well as the Kotlin-Spring dependency. You might want to in the future change the versions of both Spring Boot and Kotlin.

Now it is time to create the application, the start point of the Spring Boot Application. It looks a lot like a Java Spring Boot Application file.

package org.example;

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

class HelloKotlinSpringBootApp

fun main(args: Array<String>) {
    SpringApplication.run(HelloKotlinSpringBootApp::class.java, *args)

Next class to create is a Rest Controller which will serve GET requests.

package org.example

import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController

class GreetingController {

    fun hello(@RequestParam(value = "name", defaultValue = "World") name: String)
            = "Hello $name"

This is just a sample application so there is no need to create a more advanced example. Therefor the only thing left is to fire the application up and try it out.

Go to ‘localhost:8080/hello’ and you will see the following.


This tutorial has shown you how to create a Kotlin application in Spring Boot. There is however no difference between a Java Spring Boot Application and a Kotlin Spring Boot Application when it comes to how to run it on Raspberry Pi. You can check out this tutorial to check that out.

Whats next?

Looking for inspiration? Check out this tutorial on how to create your very own speaker using Raspberry Pi.


Kotlin Logo Icon by Icon Mafia