DEV Community

kaede
kaede

Posted on • Updated on

Kotlin SpringBoot -- Part 00 アプリを作成して起動する

why

Kotlin x Spring で Clean Architecture で REST API を作成して、テストを書きたくなった

https://qiita.com/kawasaki_dev/items/1a188878eb6928880256

これを参考にやっていく。


ゴール

  • /persons の CRUD API がクリーンなアーキテクチャで設計されている
    • 全て package が分けられている
  • Domain, Gateway, Usecase, これらに Unit テストがある
  • API E2E, EndtoEnd テストが書かれている
  • postgres DB に接続されている

Spring Initializer で Web でアプリ作成

Image description

https://start.spring.io/

このサイトで雛形が作れるらしい。

https://qiita.com/kawasaki_dev/items/1a188878eb6928880256#%E3%83%97%E3%83%AD%E3%82%B8%E3%82%A7%E3%82%AF%E3%83%88%E3%81%AE%E9%9B%9B%E5%BD%A2%E3%82%92%E4%BD%9C%E6%88%90

記事の通りに

springboot という artifact 名にして

言語を Kotlin にして

Project ビルダー?は Groovey を選択。

Spring Web ( MVC も REST も作れるらしい基礎? )
Thymleaf ( HTML を出すためのテンプレートエンジン )
Spring Data JPA ( SQL ラッパー )
H2 Database ( MySQL とかを簡単に作れるやつかな? )

これらの依存性を追加

Java は記事の 11 から 17 にする。
Springboot は 2.7.2 にした。

https://start.spring.io/#!type=gradle-project&language=kotlin&platformVersion=2.7.2&packaging=jar&jvmVersion=17&groupId=com.example&artifactId=springboot&name=springboot&description=Demo%20project%20for%20Spring%20Boot&packageName=com.example.springboot&dependencies=web,thymeleaf,data-jpa,h2

この URL を叩けば同じ構成で作成できる。
クエリにバージョンなどが入っているようだ。

Genarate ボタンを押すと
zip になって DL できる。
Ktor と同じみたい。


IntelliJ で開いてプロジェクト起動

現在なにか開いていたら Close Project してから
新たに何も開いていない状態から開く。
そうしないと

Image description

モジュールのなにも読み込まれない状態になってしまう。

Image description

全て閉じて見覚えのある JetBrains の画面から開くと
無事に読み込まれて
Spring が起動できた。

fun main(args: Array<String>) {
    runApplication<SpringbootApplication>(*args)
    println("kt")
}
Enter fullscreen mode Exit fullscreen mode

2022-08-08 07:22:40.110 INFO 1036058 --- [ main] c.e.springboot.SpringbootApplicationKt : Started SpringbootApplicationKt in 2.191 seconds (JVM running for 2.55)
kt

print を挟めばちゃんとプリントされる。


gradle の設定ファイルで依存ライブラリをみる

https://spring.pleiades.io/guides/tutorials/spring-boot-kotlin/

build.gradle.kts をみると
依存しているライブラリがわかる

plugins {
    id("org.springframework.boot") version "2.7.2"
    id("io.spring.dependency-management") version "1.0.12.RELEASE"
    kotlin("jvm") version "1.6.21"
    kotlin("plugin.spring") version "1.6.21"
    kotlin("plugin.jpa") version "1.6.21"
}
Enter fullscreen mode Exit fullscreen mode

plugins をみると基本的なライブラリがわかる

  • Spring Boot 2.7.2
  • JVM, spring のプラグイン, JPA, これらの 1.6.21

基礎ライブラリとしてこれらを使っていることが書いてある。

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-data-jpa")
    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")
    runtimeOnly("com.h2database:h2")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
}
Enter fullscreen mode Exit fullscreen mode
  • JPA
  • ThymeLeaf
  • Web
  • H2 Database

これらが拡張ライブラリとして入っていることが確認できた。

JPA, Thymleaf, H2, これらのバージョンはここでは見れないらしい。


まとめ

Kotlin で Springboot のアプリを作るには
Spring Initializer で作成&DL して
ローカルで解凍して
何も開いていない IntelliJ で開く
これだけで作成できる。


今後

https://spring.pleiades.io/guides/tutorials/spring-boot-kotlin/

Kotlin Spring のガイドラインを読む

https://qiita.com/kawasaki_dev/items/1a188878eb6928880256#%E5%8F%82%E8%80%83%E6%83%85%E5%A0%B1

MVC での CRUD を試してみる

REST で CRUD を試してみる

テストを Gauge/Selenide に変更してみる

Oldest comments (0)