Gradle + Kotlin + ThymeleafでHello World


はじめに

@Yosuke2712です。
普段はtoBの業務システムを作っています。
社内での技術のアップデードがないので個人的に色々学ぼうと思い、初めにSpring Bootで簡単なプロジェクトを作ろうとしたのですが思ったより引っかかったりしたので備忘も兼ねて作成してみました。

この記事はSpringを触ったことがない人向けです。

概要

環境
  • Windows10 Pro 1903
  • IntelliJ IDEA 2019.1.3 (Community Edition)
  • Jdk 11 (Amazon Corretto)
目次
  • Spring Initializrでプロジェクト作成
  • プロジェクトのインポート
  • Controllerの作成
  • Templateの作成
  • プロジェクトを実行

Spring Initializrでプロジェクト作成

大枠はSpring Initializrで作成します。

下記の手順通りに設定していきます。
■ Project
Gradle Project

■ Language
Kotlin

■ Spring Boot
2.1.6(2019/07/15時点)

■ Project Metadata
Group: com.example
Artifact: helloworld
(今回はこの名前を使います)
OptionsでJavaを11にします。

■ Dependencies
Developer Toolsの「Spring Boot DevTools」
Webの「Spring Web Starter」
Template Enginesの「Thymeleaf」
にチェックを入れます。

Generateからプロジェクトをダウンロードし、解凍したファイルを適当な場所に配置します。

プロジェクトのインポート

IDEAを開きます。

Import Projectを選択し、解凍したファイルを選択します。
最初の設定画面でImport project from external model > Gradleを選択します。

次の画面でUse auto-importCreate directories for empty content roots automaticallyにチェックを入れます。
※Gradle JVMの設定をミスするとうまくいかなくなります

プロジェクトを開いてbuild.gradle.ktsを確認します。
(ktsがついたのは比較的最近のようです→build.gradle.ktsに移行しよう

helloworld\build.gradle.ktsのdpendenciesは以下のようになっているはずです。

build.gradle.kts
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-reflect")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    developmentOnly("org.springframework.boot:spring-boot-devtools")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
}

devtools,web,thymeleafなどが確認できます。

Controllerの作成

helloworld/src/main/kotlin/以下にcom.example.helloworld.HelloworldApplicationがありますが、こちらは今回編集する必要はありません。
HelloworldApplicationと同じ階層にHelloworldControllerのClassを作成し、以下のように記述します。

HelloworldController.kt
package com.example.helloworld

import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.GetMapping

@Controller
class HelloworldController {

    @GetMapping("/helloworld")
    fun helloworld(model: Model): String {
        model.addAttribute("message", "Hello World!")
        return "helloworld"
    }

}

アノテーションについては下の記事にいろいろ書いてあります。
Spring FrameworkのControllerの基本的なアノテーション

Templateの作成

次にhelloworld/src/main/resources/templates以下にhtmlファイルを作成します。
xmlns:th="http://www.thymeleaf.org"
th:text="${message}"
のあたりがThymeleafに関係します。

helloworld.html
<!DOCTYPE html>
<html lang="ja" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>HelloWorld</title>
</head>
<body>
    <h1 th:text="${message}"></h1>
</body>
</html>

プロジェクトを実行

ファイルの作成が終わったら画面左上のView > Tool Windows > Gradleを開きます。

そうすると右側に↓のような画面が表示されます。
helloworld > Tasks > application > bootrunを選択するとプロジェクトが実行されます。

コンソールに以下のログが出たらhttp://localhost:8080/helloworldを開きます。
[ restartedMain] c.e.helloworld.HelloworldApplicationKt : Started HelloworldApplicationKt in 7.576 seconds (JVM running for 10.035)

このような画面が出たら成功です。

さいごに

Spring Initializrを使うとサーバー立ち上げまで簡単にできるので初心者でも取り掛かりやすいと思います。
(簡単なものであればbuild.gradleの設定などが不要のため)

参考

Spring Boot で Thymeleaf 使い方メモ