LoginSignup
5
2

More than 5 years have passed since last update.

Kotlin × joobyでREST API with API reference

Last updated at Posted at 2018-08-05

image.png

What?

シンプルで軽量なマイクロフレームワークのjoobyにKotlinで触れてみようと思う

Why?

仕事だとSpringなので気分転換に

Try

公式HP
https://jooby.org/

早速トライ
https://jooby.org/quickstart/

Kotlin gradle starterを試してみる
https://github.com/jooby-project/kotlin-gradle-starter

$ git clone https://github.com/jooby-project/kotlin-gradle-starter.git && cd kotlin-gradle-starter

$ ./gradlew joobyRun # hot reloadもしてくれる https://jooby.org/doc/devtools/#gradle-hot-reload
$ curl http://localhost:8080/
Hello Kotlin!

$ ./gradlew test # spekがstarterに入ってる

REST APIのエンドポイントを追加する

build.gradle
dependencies {
    ~~~
    compile "org.jooby:jooby-jackson:$joobyVersion"
    ~~~
}
App.kt
import org.jooby.json.Jackson

data class Hello(val hello: String)

class App : Kooby({
    use(Jackson())
    ~~~
    get("/hello/{name}") { Hello(param("name").value()) }
})
$ ./gradlew joobyRun
$ curl http://localhost:8080/hello/taro | jq
{
  "hello": "taro"
}

API referenceを導入してみる

build.gradle
dependencies {
    ~~~
    compile "org.jooby:jooby-apitool:$joobyVersion"
    ~~~
}
App.kt
import org.jooby.apitool.ApiTool

class App : Kooby({
    ~~~
    use(ApiTool().swagger().raml())
    ~~~
})
$ ./gradlew joobyRun
$ curl http://localhost:8080/swagger
$ curl http://localhost:8080/raml

Swagger

スクリーンショット 2018-08-10 22.26.08.png

RAML

スクリーンショット 2018-08-10 22.26.37.png

感想

  • 簡単
  • 周辺ツールも結構ある
  • ちょっとしたAPI作るぐらいなら良さそう
  • Springよりかわいい
5
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2