0
0

VSCodeでSpring Bootを利用してプロジェクトを新規作成する

Last updated at Posted at 2024-01-20

初めに

新規参画したプロジェクトでSpring Bootを利用していたため、クイックスタートを参考にVSCodeでプロジェクトを新規に作成する

クイックスタート

手順に従ってプロジェクトを開始する
(設定内容は画面から読み取れる設定を反映した)

新プロジェクト開始

Spring Bootで新しいプロジェクトを作成する。
→私はVSCodeで開発しているため、特に説明はなかったが拡張で以下を追加してプロジェクトの作成を実行した。

  • Spring Boot Extension Pack

新規プロジェクトだと以下の画面が表示されるため、「Getting Started with Spring Boot in VS Code」を実行する。
image.png

「Create New Project」を押して初期設定

  • バージョン:3.2.2
  • 言語: Java
  • グループID: com.example
  • アーティファクトID: demo
  • パッケージングタイプ: jar
  • Javaバージョン: 17
  • 依存関係: "Spring Boot DevTools", "Spring Web"

→上記の設定により以下のディレクトリが作成された
image.png

コード追加

Helloを出力するコードを追加する。
コードから以下が読み取れた。

  • @SpringBootApllication: アプリケーションの開始宣言
  • @RestController: エンドポイントの指定
  • @GetMapping: pathを指定してマッピング
  • @RequestParam: Requestのパラメータを指定
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class DemoApplication {
    public static void main(String[] args) {
      SpringApplication.run(DemoApplication.class, args);
    }
    @GetMapping("/hello")
    public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
      return String.format("Hello %s!", name);
    }
}

実行

アプリケーションの実行
→ VSCodeでは以下から実行
 image.png

以下にアクセスしてHelloが出力されることを確認
http://localhost:8080/hello

image.png

@RequestParamnameを宣言していたため、Requestのパラメータを指定すると出力文字が変更されることを確認
http://localhost:8080/hello?name=spring
image.png

0
0
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
0
0