はじめに
やること
- VSCodeでSpring Bootを含んだJavaプロジェクトを作る
環境
- Java 17
- Spring Boot 3.0.6
- Gradle
手順
Spring Boot Extension Packをインストールする。
CTRL+Shift+P
で検索欄を開き、Spring Initializr: Create a Gradle Project
を押下。
グループIDを入力。com.<userID>
とかでいいと思います。
依存関係を選択。
Webを構築したい場合はSpring Web
とThymeleaf
を選択しとけばよいと思います。他はプロジェクトに合わせて。
最後に、プロジェクトを作成するフォルダの場所を指定。
右下にSuccessfully
が出たらOK。
これでプロジェクトは完成です。
実行する
example/controller
にHelloController.java
を、resources/templates
にhello.html
を作成する。
内容は以下の通り。
HelloController.java
package com.example.springbootdemo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
@RequestMapping("/hello")
public String index() {
return "hello";
}
}
hello.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello!</title>
</head>
<body>
<h1>Hello!</h1>
</body>
</html>
実行する際は、「実行>デバッグの開始」を押す。
一度目はうまくいかない場合があるので注意。その際はもう一度実行しなおす。
http://localhost:8080/helloにアクセスしてみて、以下のような表示が出れば成功。