kotlin+springをSpring Initializrを使ってサクッとhello worldまで

とりあえず今回は以下のバージョンで行いました

  • spring:2.0.0

Generate Projectを押すとzipファイルが落ちてきます. それを解答して,開発を始めます.

Controllerの追加

DemoApplicationが指定したgroupID, ArtifactIDの階層にあると思うので,そこと同じ階層にDemoControllerを作成します.

package com.example.demo

import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RestController

@RestController
class CustomerController() {

    @GetMapping("/")
    fun hello() = "hello"

    @GetMapping("/{name}")
    fun helloName(@PathVariable name:String)
            = name
}

ルーティング

  • 今回は以下のようなルーティングにした
Method Route
GET /
GET /{name}

あとはgradle bootRunで起動する.http://localhost:8080にアクセスすると確認できる.