0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

SpringBootでHelloWorld(2022年3月26日時点)

Posted at

はじめに

Spring BootでHello,WorldするSpring Quickstart Guideの内容について、環境セットアップ面を補足した手順を作成。

環境

OS:Windows10 Home

手順

  1. Eclipse 2020のインストール
    ①javaのFull Editionを使用
    https://mergedoc.osdn.jp/
    ②exeを実行して、c:\pleiades\2022-03に解凍

  2. Srping InitializrでSpringBootプロジェクトを作成
    ①以下のページにアクセス
    https://start.spring.io/
    ②Dependenciesに「Spring Web]を追加し、それ以外はデフォルトの設定でGENERATE
    image.png
    ③生成されたdemo.zipをC:\workで解凍

  3. SpringBootプロジェクトのEclipseへのインポート
    ①Eclipseのexe実行
    "C:\pleiades\2022-03\eclipse\eclipse.exe"
    ②ワークスペースディレクトリはデフォルト
    image.png
    ③[ファイル]-[インポート]を選択
    image.png
    ④[Maven]-[既存のMavenプロジェクト]を選択
    image.png
    ⑤解凍したdemoフォルダを選択。"ワーキング・セットへプロジェクトを追加"に☑。[完了]を選択。
    image.png
    ⑥完了
    image.png

  4. コードの編集
    ①DemoApplication.javaを開く
    ②以下の通り編集する

    DemoApplication.java(編集後)
    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);
    +	}
    
    }
    
    DemoApplication.java(編集前)
    
    package com.example.demo;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class DemoApplication {
    
    	public static void main(String[] args) {
    		SpringApplication.run(DemoApplication.class, args);
    	}
    
    }
    
  5. Maven実行用JDKのインストール
    ①Liberica JDKのWindows用JDK11のzip版をダウンロード。C直下で解凍して、C:\jdk-11.0.14.1のパスに展開。
    https://bell-sw.com/pages/downloads/
    image.png
    ②コマンドプロンプトを起動して、setx JAVA_HOME C:\jdk-11.0.14.1を実行して、ユーザ環境変数JAVA_HOMEを設定。
    image.png
    ③コマンドプロンプトをexitで終了し、再度起動すると設定したユーザ環境変数が使用できるようになる。

  6. SpringBootアプリの実行

①コマンドプロンプトを起動。C:\work\demoディレクトリへ移動。
mvnw spring-boot:runを実行
image.png
②SpringBootアプリが以下の通り起動
image.png
③ブラウザを開いてhttp://localhost:8080/helloと入力するとレスポンスメッセージ"Hello World!"が表示される。
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?