Building web applications with Spring
Boot and Kotlin
This tutorial shows you how to build efficiently a sample blog application by
combining the power of Spring Boot and Kotlin.
If you are starting with Kotlin, you can learn the language by reading the reference
documentation, following the online Kotlin Koans tutorial or just using Spring
Framework reference documentation which now provides code samples in Kotlin.
Spring Kotlin support is documented in the Spring Framework and Spring
Boot reference documentation. If you need help, search or ask questions with
the spring and kotlin tags on StackOverflow or come discuss in
the #spring channel of Kotlin Slack.
Creating a New Project
First we need to create a Spring Boot application, which can be done in a number of
ways.
Using the Initializr Website
Visit https://start.spring.io and choose the Kotlin language. Gradle is the most
commonly used build tool in Kotlin, and it provides a Kotlin DSL which is used by
default when generating a Kotlin project, so this is the recommended choice. But you
can also use Maven if you are more comfortable with it. Notice that you can
use https://start.spring.io/#!language=kotlin&type=gradle-project-kotlin to have Kotlin
and Gradle selected by default.
1. Select "Gradle - Kotlin" or "Maven" depending on which build tool you want to
use
2. Enter the following artifact coordinates: blog
3. Add the following dependencies:
Spring Web
Mustache
Spring Data JPA
H2 Database
Spring Boot DevTools
4. Click "Generate Project".
The .zip file contains a standard project in the root directory, so you might want to
create an empty directory before you unpack it.
Using command line
You can use the Initializr HTTP API from the command line with, for example, curl on
a UN*X like system:
COPY$ mkdir blog && cd blog
$ curl https://start.spring.io/starter.zip -d language=kotlin -d
type=gradle-project-kotlin -d
dependencies=web,mustache,jpa,h2,devtools -d
packageName=com.example.blog -d name=Blog -o blog.zip
Add -d type=gradle-project if you want to use Gradle.
Using IntelliJ IDEA
Spring Initializr is also integrated in IntelliJ IDEA Ultimate edition and allows you to
create and import a new project without having to leave the IDE for the command-
line or the web UI.
To access the wizard, go to File | New | Project, and select Spring Initializr.
Follow the steps of the wizard to use the following parameters:
Artifact: "blog"
Type: "Gradle - Kotlin" or "Maven"
Language: Kotlin
Name: "Blog"
Dependencies: "Spring Web Starter", "Mustache", "Spring Data JPA", "H2
Database" and "Spring Boot DevTools"
Understanding the Gradle Build