LoginSignup
2
4

More than 5 years have passed since last update.

Spring Boot で Hello, world! するのを Java と Kotlin と Groovy で比較する

Last updated at Posted at 2017-06-18

Spring Boot で Hello, world! するのを Java と Kotlin と Groovy で比較してみたのでメモっとく。

なお本稿は macOS 環境を前提にしている。

Spring Boot CLI をインストールする

Homebrew で Spring Boot CLI をインストールする。

$ brew tap pivotal/tap
$ brew install springboot

ターミナルで下記のコマンドを実行してみるとバージョンが表示される。なお、Rails 環境がローカルにあると Rails が使用している spring が実行されてしまう可能性があるので PATH 環境変数を調整しよう。

$ spring --version
Spring CLI v1.5.4.RELEASE

Hello プロジェクトを作成する

下記のコマンドで Hello プロジェクトを作成する。ビルドツールとして Gradle を使用し、依存に Web を追加し、言語として Java を使用し、Hello という名前のプロジェクトとして hello ディレクトリに展開する。

$ spring init --build=gradle -d=web -l=java -n=hello hello

-l=java-l=kotlin-l=groovy にすることで Kotlin や Groovy でコードが生成される。

プロジェクトのコードは適当なテキストエディターで編集して構わないが、IntelliJ IDEA Ultimate や IntelliJ IDEA Community で編集したい人は下記のコマンドでプロジェクトディレクトリを開くといい。

$ idea hello

Hello, world! を実装する

Java と Kotlin と Groovy で Hello, world! を実装してみる。

src/main/java/com/example/hello/HelloApplication.java
package com.example.hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class HelloApplication {

    public static void main(String[] args) {
        SpringApplication.run(HelloApplication.class, args);
    }

    @RequestMapping("/")
    String hello() {
        return "Hello, world!";
    }
}
src/main/kotlin/com/example/hello/HelloApplication.kt
package com.example.hello

import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@SpringBootApplication
@RestController
class HelloApplication {
    @RequestMapping("/")
    fun hello() = "Hello, world!"
}

fun main(args: Array<String>) {
    SpringApplication.run(HelloApplication::class.java, *args)
}
src/main/groovy/com/example/hello/HelloApplication.groovy
package com.example.hello

import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@SpringBootApplication
@RestController
class HelloApplication {

    static main(args) {
        SpringApplication.run HelloApplication, args
    }

    @RequestMapping("/")
    def hello() {
        "Hello, world!"
    }
}

このくらいのコードだとあまり差がないなぁ。

Hello プロジェクトを実行する

Hello プロジェクトのディレクトリに移動して Gradle で bootRun タスクを実行すると Web サーバーが起動する。

$ ./gradlew bootRun

GET リクエストを送信する

起動した Hello プロジェクトに GET リクエストを送信すると Hello, world! が返ってくる。

$ curl localhost:8080
Hello, world!

今回は Spring Boot と Java/Kotlin/Groovy で Hello, world! してみた。発端はマイクロフレームワークとして Spring Boot いいんじゃないかと思って試してみたんだけど、import 文があるからかマイクロって感じに欠ける結果に……。Spring Boot はマイクロなところから始めてフルスタックに進化していけるいい環境だと思うんで、色々知らなくても Spring で遊べるような情報が増えるといいなぁ。

参考文献

2
4
2

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
2
4