REST-APIの最初のコードとしてURLに入力したIDをGETするだけのAPIを作ってみる。
Controller層用(API受付層)のパッケージ作ってKotlinファイルを追加

エンドポイントはhttp://localhost:8080/items/12345のような感じにしてみる。
12345を入力としてそのままそのIDを返すだけのAPI。
@RestController
@RequestMapping("/items")
class ItemListController {
@GetMapping("/{id}")
@ResponseBody
@ResponseStatus(HttpStatus.OK)
fun getItem(@PathVariable("id") id: Int): String {
return "item id = $id"
}
}
ブラウザでlocalhostにアクセスしてみる。port番号はデフォルト8080。

コメントを残す