Kotlin + Spring Boot 2.1.0 + Thymeleaf + IntelliJ IDEA + Gradle で Hello World


概要

IntelliJ IDEA で Kotlin + Spring Boot 2.1.0 + Thymeleaf を使用して Hello World を表示するプログラムを書く。

環境

  • IntelliJ IDEA Community Version 2018.2.5
  • Kotlin plugin version 1.3.0-release-IJ2018.2-1
  • Gradle 4.8.1
  • Spring Boot 2.1.0
  • Thymeleaf 3.0.11

Spring Initializr でプロジェクトの雛形を生成する

Spring Initializr で以下の内容を指定して 「Generate Project」 する。

  • Generate a 「Gradle Project」 with 「Kotlin」 and Spring Boot 「2.1.0」
  • Group: com.example
  • Artifact: demo
  • Dependencies: Web, Thymeleaf

プロジェクトの zip ファイルをダウンロードできる。

プロジェクトファイル

ダウンロードした zip ファイルを展開。

├── build.gradle
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
└── src
    ├── main
    │   ├── kotlin
    │   │   └── com
    │   │       └── example
    │   │           └── demo
    │   │               └── DemoApplication.kt
    │   └── resources
    │       ├── application.properties
    │       ├── static
    │       └── templates
    └── test
        └── kotlin
            └── com
                └── example
                    └── demo
                        └── DemoApplicationTests.kt

ビルド用ファイル build.gradle

今回は Kotlin 1.3.0 を使用しているので kotlinVersion = '1.3.0' に書き直しても良いが、修正しなくても良い。

buildscript {
    ext {
        kotlinVersion = '1.2.70'
        springBootVersion = '2.1.0.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}")
        classpath("org.jetbrains.kotlin:kotlin-allopen:${kotlinVersion}")
    }
}

apply plugin: 'kotlin'
apply plugin: 'kotlin-spring'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
compileKotlin {
    kotlinOptions {
        freeCompilerArgs = ["-Xjsr305=strict"]
        jvmTarget = "1.8"
    }
}
compileTestKotlin {
    kotlinOptions {
        freeCompilerArgs = ["-Xjsr305=strict"]
        jvmTarget = "1.8"
    }
}

repositories {
    mavenCentral()
}


dependencies {
    implementation('org.springframework.boot:spring-boot-starter-thymeleaf')
    implementation('org.springframework.boot:spring-boot-starter-web')
    implementation('com.fasterxml.jackson.module:jackson-module-kotlin')
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    testImplementation('org.springframework.boot:spring-boot-starter-test')
}

アプリケーションクラス DemoApplication.kt

特に何も修正しない。

package com.example.demo

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication

@SpringBootApplication
class DemoApplication

fun main(args: Array<String>) {
    runApplication<DemoApplication>(*args)
}

IntelliJ IDEA でプロジェクトを作成する

IntelliJ IDEA にて Import Project で build.gradle を指定する。

Use auto-import にチェックを入れてインポート。

雛形プログラムを実行する

IntelliJ IDEA のメニューにて View → Tool Windows → Gradle で Gradle のウィンドウを開く。

Gradle のウィンドウにて Tasks → application → bootRun で右クリックして Run 'demo [bootRun]' を選択して実行。

実行すると Run ウィンドウに以下のような Spring Boot のメッセージが表示される。

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.0.RELEASE)

その後「Tomcat started on port(s): 8080 (http) with context path ''」や「Started DemoApplicationKt」というようなログが表示される。

http://localhost:8080 にアクセスすると以下のようなメッセージが表示される。サーバが起動してはいるが、プログラムの中身が無い状態になっている。

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Hello World のコードを追加する

2つのファイル HelloController.kt と hello_name.html を追加して Hello World を実現する。

Controller クラスを追加する

以下の HelloController クラスを src/main/kotlin/com/example/demo/HelloController.kt に追加する。

package com.example.demo

import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.ResponseBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod

@Controller
class HelloController {

    @RequestMapping(value = ["/hello"], method = [RequestMethod.GET])
    @ResponseBody
    fun hello_world(): String {
        return "hello, world"
    }

    @RequestMapping(value = ["/hello/{name}"], method = [RequestMethod.GET])
    fun hello_name(@PathVariable name: String, model: Model): String {
        model.addAttribute("name", name)
        return "hello_name" // Thymeleaf テンプレートファイル名
    }
}

Thymeleaf のテンプレートファイルを追加する

以下の HTML テンプレートファイルを src/main/resources/templates/hello_name.html に追加する。

<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <title>Hello, [[${name}]]</title>
</head>
<body>
<p>Hello, [[${name}]]</p>
</body>
</html>

Hello World プログラムを実行する

再度、プログラムを実行する。

http://localhost:8080/hello にアクセスすると、「hello, world」と表示される。

http://localhost:8080/hello/hoge にアクセスすると、「Hello, hoge」と表示される。

URLの「hoge」の部分は任意の文字列を指定できる。

参考資料