Spring BootをGradleで作成する方法

Spring BootをGradleで作成する方法

STSのSpringスタータープロジェクトが昔と違ってかなり進化しているので、Gradleで簡単なRest API(REpresentational State Transfer)を作ってみます。

Spring BootをGradleで作成する方法

「次へ」をクリックします。

Spring BootをGradleで作成する方法

「完了」を選択すると、jp.co.confrageパッケージにSts2Application.javaが作成されています。中身は以下の通りです。

package jp.co.confrage;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

次にRestControllerを作成します。HTMLを返す場合は@Controllerですが、文字を返すだけなので@RestControllerにしています。

package jp.co.confrage;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Sts2Controller {

  @RequestMapping("/")
  public String home() {
    return "test-desu";
  }
}

これでプロジェクトを右クリックし、「実行」-「Spring Boot アプリケーション」を選択して、http://localhost:8080/にアクセスすれば、文字が表示されます。

Spring BootをGradleで作成する方法

コメント

タイトルとURLをコピーしました