0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Java】Spring Bootの環境構築(Web UI 版)

Posted at

Spring Boot の環境構築 を Web UI(Spring Initializr)を利用して行う備忘録です。ぜひ参考にしてください。
環境はApple M1 Pro、java17です。

1. Spring Boot プロジェクトの作成

1.1 Spring Initializr にアクセス

以下の URL にアクセスします。

Spring Initializr

1.2 プロジェクトの設定

設定項目
Project Maven
Language Java
Spring Boot Version 3.4.2
Group com.example
Artifact spring-boot-app
Name spring-boot-app
Package Name com.example.spring-boot-app
Packaging Jar
Java Version 17

1.3 依存関係の追加

プロジェクトの依存関係(Dependencies)を選択します。

  • Spring Web(REST API を作成するため)
  • Lombok(ボイラープレートコードを削減)
  • Spring Boot DevTools(開発用のホットリロード機能)

1.4 プロジェクトの解凍

Generate ボタンをクリックし、ZIP ファイルをダウンロードします。

ダウンロードした spring-boot-app.zip を解凍し、作成されたフォルダを開きます。

unzip spring-boot-app.zip
cd spring-boot-app

2. プロジェクトの起動

2.1Maven の依存関係をインストール

プロジェクトのルートディレクトリで以下のコマンドを実行し、Maven の依存関係をダウンロードします。

./mvnw clean install

2.2Spring Boot の起動

以下のコマンドで Spring Boot アプリケーションを起動します。

./mvnw spring-boot:run

成功すると、以下のようなログが表示されます。

started on port 8080 (http) with context path '/'

ブラウザで http://localhost:8080 にアクセスすると、アプリケーションが動作していることが確認できます。
まだルーティングを設定していないのでWhitelabel Error Pageが出ます。

動作確認ようのAPI作成

環境構築が完了したら、簡単な REST API を作成して動作を確認しましょう。

3.1 コントローラーの作成

src/main/java/com/example/springbootapp/controller/HelloController.java を作成し、以下のコードを追加します。

package com.example.springbootapp;

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

@RestController
@RequestMapping("/api")
public class HelloController {

    @GetMapping("/hello")
    public String hello() {
        return "Hello, Spring Boot!";
    }
}

3.2 API の動作確認

ブラウザで http://localhost:8080/api/hello にアクセスすると以下のレスポンスが表示されます。

{
  "message": "Hello, Spring Boot!"
}

4まとめ

✅ Spring Initializr を使用してプロジェクトを作成
✅ Maven で依存関係をインストール
✅ Spring Boot の起動と API 動作確認

この手順で Spring Boot の環境構築が完了しました!
次は、実際のアプリケーション開発を進めていきましょう。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?