LoginSignup
27
24

More than 5 years have passed since last update.

Spring Bootを使って35秒でAPIを作成する(mac編)

Last updated at Posted at 2015-11-30

最近世間でもてはやされているSpring Bootですが、これを使って35秒でAPIを作ってみようと思います。

まずはspringbootをbrewでインストール

(brewは環境によってかかる時間など異なるのでこれはまだ35秒に入れません・・・)

brew tap pivotal/tap
brew install springboot

35秒の計測スタート!

まずはgroovyのファイル作成

vi api.groovy

ファイルの内容は以下をコピペ

api.groovy
@RestController
class ThisWillActuallyRun {
    @RequestMapping("/")
    String home() {
        "Hello World!"
    }
}

springコマンドでAPIを起動

spring run api.groovy

(初回起動の時だけ依存関係の解決に時間がかかります・・・ごにょごにょ)

したらブラウザから以下のURLで繋がります!ここまで35秒!

http://localhost:8080/

Hello World!

皆さんどうですか?35秒でAPI作れましたよね?

え?間に合わなかったって??きっとコピペとか、タイピングとか訓練すれば35秒でいけます!!

ちなみにgetパラメータとかもらう場合はこんな感じで書けます

api.groovy
@RestController
class ThisWillActuallyRun {
    @RequestMapping("/")
    String home(@RequestParam(value="name") String name) {
        "Hello $name"
    }
}

http://localhost:8080/?name=hanako

Hello hanako

springbootとgroovy組み合わせると、ファイル1つでAPI出来てとっても簡単で良いですね!!

27
24
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
27
24