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がチュートリアル通りでうまくいかなかったので別の方法で何とか動かした

Posted at

javaの勉強を始めて、フレームワークも触ってみたいということで
Springに挑戦しました。

チュートリアル通りにやってみて、ローカルサーバーを立ち上げのところで
エラーが解消せずうまくいかなかったので、別の方法で動かせたので、参考なるか分かりませんが
メモを残します。

公式のチュートリアル
https://spring.io/quickstart

結論:MavenでプロジェクトをGenerateする

チュートリアルでは、Gradle - Groovyでプロジェクトをビルドする流れとなっておりますが
私はこれだとビルドとローカルサーバーの立ち上げのところでエラーが出て直りませんでした。
(javaのバージョン不一致のエラーでしたが、バージョンを合わせても解決せず)
そこで、MavenでプロジェクトをGenerateすしたところうまくいきました。

詳細な手順は、この後解説します。

STEP1:プロジェクトをGenerate

結論で言ったように、MavenでプロジェクトをGenerateします。
以下のように設定して"Generate"ボタンを押してください。
(※私はjave version=24ですが、verは自身の環境と一致するのもにしてください)

sぴりん.jpg

STEP2:コード編集

ここは公式チュートリアルと全く同じでいいです。
src/main/java/com/example/demoの中の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);
    }
}

STEP3:ビルド&実行(サーバー起動)

MavenでプロジェクトをGenerateした場合、ここが公式のチュートリアルと手順が異なります。
プロジェクトの階層で、下記のコマンドを実行します。

./mvnw spring-boot:run --quiet

これで成功しました。
ぶlるん.png

以上です。

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?