Thiago Teixeira
Thiago Teixeira Senior Software Engineer

How to Create a Spring Boot Application using Kotlin and Gradle

How to Create a Spring Boot Application using Kotlin and Gradle

A few months ago I wrote an article showing how to create a simple Spring Boot application using Java, you can see it by clicking here.

Now, I am going to show you, how to create the same application using Kotlin and Gradle.

The requirements are:

Now, you must go to the following website:

Choose to generate a Gradle Project with Kotlin and fill in the group and the artifact fields. Example:

  • Group: com.mydomain
  • Artifact: demo

In the Search for dependencies field fill in: Web

The options chosen will be as the image below:

Spring Initializr Kotlin option

Now, click on the Generate Project button

So, download the ZIP file and unzip it.

Using a text editor ( I recommend Visual Studio Code), open the demo folder.

We are going to create the web controller file named HelloWorldController.kt at demo\src\main\kotlin\com\example\demo\\ folder:

Spring Boot Kotlin Controller Hello World

Your respective code will be:

package com.example.demo

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

class HelloWorldController {

    fun index() = "Hello, World!"


Now, using the command prompt you can initialize the application using the command: gradle bootrun. It is going to use Gradle. After the execution, you will see the success message referring to the initialization from an embedded Apache Tomcat.

** <==========—> 80% EXECUTING [1m 40s]

:bootRun ** **

So, access http://localhost:8080 in the web browser.

Spring Boot Kotlin Controller Hello World in Browser

Finally, your first Spring Boot application with Kotlin and Gradle was created.

Thanks for reading.

comments powered by Disqus