Kotlin 1.5.0 Help

Create a RESTful web service with Spring Boot – tutorial

This tutorial walks you through the process of creating a simple application with Spring Boot.

You will create an application with an HTTP endpoint that returns a data objects list in JSON format.

This tutorial consists of two parts:

To get started, first download and install the latest version of IntelliJ IDEA.

You can also watch a video of this tutorial:

Bootstrap the project

Use Spring Initializr to create a new project:

  1. Open Spring Initializr. This link opens the page with the project settings for this tutorial already filled in. This project uses Gradle, Kotlin, Spring Web, Spring Data JDBC, and H2 Database:

    Create a new project with Spring Initializr

  2. Click GENERATE at the bottom of the screen. Spring Initializr will generate the project with the specified settings. The download starts automatically.

  3. Unpack the .zip file and open it in IntelliJ IDEA.

    The project has the following structure:

    The Spring Boot project structure

    There are packages and classes under the main/kotlin folder that belong to the application. The entry point to the application is the main() method of the DemoApplication.kt file.

Explore the project build file

Open the build.gradle.kts file.

This is the Gradle Kotlin build script, which contains a list of the dependencies required for the application.

The Gradle file is standard for Spring Boot, but it also contains necessary Kotlin dependencies, including the kotlin-spring Gradle plugin.

Explore the Spring Boot application

Open the DemoApplication.kt file:

package demo import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.boot.runApplication @SpringBootApplication class DemoApplication fun main(args: Array<String>) { runApplication<DemoApplication>(*args) }

Note that the Kotlin application file differs from a Java application file:

  • While Spring Boot looks for a public static main() method, the Kotlin application uses a top-level function defined outside DemoApplication class.

  • The DemoApplication class is not declared as open, since the kotlin-spring plugin does that automatically.

Create a data class and a controller

To create an endpoint, add a data class and a controller to your project:

  1. In the DemoApplication.kt file, create a Message data class with two properties: id and text:

    data class Message(val id: String?, val text: String)
  2. In the same file, create a MessageResource class which will serve the requests and return a JSON document containing a collection of Message objects:

    @RestController class MessageResource { @GetMapping fun index(): List<Message> = listOf( Message("1", "Hello!"), Message("2", "Bonjour!"), Message("3", "Privet!"), ) }

Full code of the DemoApplication.kt:

package demo import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.boot.runApplication import org.springframework.data.annotation.Id import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.RestController @SpringBootApplication class DemoApplication fun main(args: Array<String>) { runApplication<DemoApplication>(*args) } @RestController class MessageResource { @GetMapping fun index(): List<Message> = listOf( Message("1", "Hello!"), Message("2", "Bonjour!"), Message("3", "Privet!"), ) } data class Message(val id: String?, val text: String)

Run the application

The application is now ready to run:

  1. Click the green Run icon in the gutter beside the main() method or use the Alt+Enter shortcut to invoke the launch menu in IntelliJ IDEA:

    Run the application

  2. Once the application starts, open the following URL: http://localhost:8080.

    You will see a page with a collection of messages in JSON format:

    Application output

Proceed to the next tutorial

In the next part of this tutorial you will add a database for storing objects, as well as two endpoints for writing and retrieving those objects: Add a database to a Spring Boot RESTful web service.

Last modified: 19 April 2021