创建一个基于Sprint Boot的 RESTful Web服务

本教程将教会大家如何使用Kotlin搭建一个基于Sprint Boot的的REST服务。

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 RESTful services, please see Building a RESTful Web Service.

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.1.4-3' // Required for Kotlin integration
    ext.spring_boot_version = '1.5.4.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#kotlin-spring-compiler-plugin
        classpath "org.springframework.boot:spring-boot-gradle-plugin:$spring_boot_version"
    }
}

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

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.

创建Application类

最后,我们需要创建一个Application类。在Spring Boot中需要创建一个 public staticmain 函数,作为入入口。在Kotlin语言之中,我们同样定义这样一个,并在 class 上标识 @JvmStatic 注解。注意,在Kotlin之中Main函数已经是一个顶层函数,加入class是为了加入注解,是由于Spring Boot的注解要求。

值得注意的是,这里Application class 不需要标识 open,我们可以kotlin-spring的Gradle插件自动配置。

@SpringBootApplication
class Application

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

运行应用程序

现在我们可以使用所有的Gradle tasks来启动Spring Boot应用了,这里我们使用最常见的:

./gradlew bootRun

在应用程序编译、打包、启动后,我们就可以通过默认端口8080在浏览器中看到了。

image