LoginSignup
5
4

More than 1 year has passed since last update.

VSCodeでSpringBootアプリの環境構築をやってみた

Posted at

Javaに触れようと思い、まずは環境を構築しないと、と考えていた矢先、

「簡単なSpringBootWebアプリケーションを作成し、それをローカルにテストする為に必要な手順」が解説されているWebページをいくつか見かけたので、実際にVSCodeでも試してみました。

事前準備として、私は必要なツールを揃えるところから始めました。

・エディターまたは統合開発環境 (IDE) :Visual Studio Code
Extension Pack for Java(VSCode拡張機能)
・Spring Boot Extension Pack(VSCode拡張機能)

・JDK(Java 17)   https://www.oracle.com/jp/java/  :端末にインストール

・端末環境:Ubuntu / Linux 用 Windows サブシステム(WSL2推奨)
・git

参考)VS Code を使用してコンテナーでの開発をする場合
https://code.visualstudio.com/docs/devcontainers/containers#_installation

[手順]:VSCodeでSpring Boot アプリを作成する

まず、VSCodeでコマンドパレットに「Spring Initializr」 コマンドを入力し、Spring Boot のWeb開発用プロジェクトを作成します。
image.png
Project:Maven Project / Language:Java / GroupID:com.example
ArtifactID:demo / PackagingType:Jar / Java version:17

依存関係はここでは、以下の4つを選択します。
image.png
次に、OpenJDKをインストールします。
WSL環境でOpenJDKを設定する場合、ターミナルで

$sudo apt-get install openjdk-17-jdk

と入力し、以下のURLページの設定をしてください。
参考URL:https://kontext.tech/article/621/install-open-jdk-on-wsl

次に、ControllerとViewを作成して、Hello Worldを画面表示していきます。

DemoApplication.java
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
@SpringBootApplication
public class DemoApplication {

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

	@GetMapping("/")
	public String helloWorld() {
		return "index";
	}
}

src/main/resources/templates/index.html
<html>
  <head>
    <meta charset="UTF-8" />
    <title>hello world</title>
  </head>
  <body>
    <p>Hello World</p>
  </body>
</html>

demoプロジェクトを右クリック⇒Run して「localhost:8080」でHello Worldと表示されるのを確認します。

image.png
出来ました!

今後、VSCodeでもSpringBootアプリケーションの開発をしていけると良いなと考えています。

以上となります。

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