W3cubDocs

/Kotlin

Creating a RESTful Web Service with Spring Boot

Author Hadi Hariri, Edoardo Vacchi, Sébastien Deleuze
Source On GitHub
This tutorial walks us through the process of creating a simple REST controller with Spring Boot

Kotlin works quite smoothly with Spring Boot and many of the steps found on the Spring Guides for creating a RESTful service can be followed verbatim for Kotlin. There are some minor differences however when it comes to defining the Gradle configuration and the project layout structure, as well as the initialization code.

In this tutorial we'll walk through the steps required. For a more thorough explanation of Spring Boot and Kotlin, please see Building web applications with Spring Boot and Kotlin.

Note that all classes in this tutorial are in the org.jetbrains.kotlin.demo package.

Defining the project and dependencies

In this tutorial, we're going to be using Gradle but the same can be accomplished using either IntelliJ IDEA project structure or Maven. For details on setting up Gradle to work with Kotlin, see Using Gradle.

The Gradle file is pretty much standard for Spring Boot. The only differences are the structure layout for source folders for Kotlin, the required Kotlin dependencies and the kotlin-spring Gradle plugin (CGLIB proxies used for example for @Configuration and @Bean processing require open classes).

buildscript {
    ext.kotlin_version = '1.4.10' // Required for Kotlin integration
    ext.spring_boot_version = '2.1.0.RELEASE'
    repositories {
        jcenter()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // Required for Kotlin integration
        classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version" // See https://kotlinlang.org/docs/reference/compiler-plugins.html#spring-support
        classpath "org.springframework.boot:spring-boot-gradle-plugin:$spring_boot_version"
	classpath "io.spring.gradle:dependency-management-plugin:1.0.6.RELEASE"
    }
}

apply plugin: 'kotlin' // Required for Kotlin integration
apply plugin: "kotlin-spring" // https://kotlinlang.org/docs/reference/compiler-plugins.html#spring-support
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

jar {
    baseName = 'gs-rest-service'
    version = '0.1.0'
}

repositories {
    jcenter()
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" // Required for Kotlin integration
    compile "org.springframework.boot:spring-boot-starter-web"
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

Creating a Greeting Data Class and Controller

The next step is to create Greeting Data class that has two properties: id and a content

data class Greeting(val id: Long, val content: String)

We now define the GreetingController which serves requests of the form /greeting?name={value} and returns a JSON object representing an instance of Greeting

@RestController
class GreetingController {

    val counter = AtomicLong()

    @GetMapping("/greeting")
    fun greeting(@RequestParam(value = "name", defaultValue = "World") name: String) =
            Greeting(counter.incrementAndGet(), "Hello, $name")

}

As can be seen, this is again pretty much a one-to-one translation of Java to Kotlin, with nothing special required for Kotlin.

Creating the Application class

Finally we need to define an Application class. As Spring Boot looks for a public static main method, we need to define this in Kotlin. It could be done with the @JvmStatic annotation and a companion object but here we prefer using a top-level function defined outside Application class since it leads to more concise and clean code.

No need to mark the Application class as open since we are using the kotlin-spring Gradle plugin which does that automatically.

@SpringBootApplication
class Application

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

Running the application

We can now use any of the standard Gradle tasks for Spring Boot to run the application. As such, running

./gradlew bootRun

the application is compiled, resources bundled and launched, allowing us to access it via the browser (default port is 8080)

Running App

© 2010–2020 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/docs/tutorials/spring-boot-restful.html