1
4

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 5 years have passed since last update.

Spring Boot の学習【はじめ】

Posted at

仕事でJavaは少しくらいは触っているけど、流行りのフレームワークには触れる機会がなく過ごしていますが何となくSpring Bootを見ていると、もしかしたら今度こそこれがスタンダードのフレームワークとして定着するんじゃないかなぁ?覚えて損はない。と思い立って、触ってみる事にしました。

【スターター・プロジェクト】

はじめてのSpring Bootプロジェクト作成ですが、他に選択が無いので定番かもしれませんがここからスタートします。

1.プロジェクト設定1
Gradleというのがどうも筆者は設定が見やすいので、これにします。
パッケージは、jarはまだよくわからないのでwarとします。

start_1.png

2.プロジェクト設定2
とりあえずは、動かしてみたいだけなのでテンプレートエンジンとwebだけを選択
たぶん後で追加はできるはず。
start_2.png

3.自動生成の中身
下記のように自動生成されました。
start_3.png

4.コントローラーの作成
Controllerは主にWebページ用のコントローラで使用する。
RestControllerはJsonやXML等を返すWebAPI用のコントローラで使用する。
との事ですが、どっちも試してみたいと思います。
まずは、RestControllerから作ります。

TestRest.java
package com.example.controller;

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

@RestController
public class TestRest {

	@RequestMapping("/Rest")
	public String Rest() {
		return "これはRest";
	}

}

start_4.png

5.実行してみる
実行で「Spring Bootアプリケーション」を選択する
start_5.png

6.ブラウザーからアクセスしてみる
どうもTomcatは動いているが、肝心の作成したコントローラが見つからない?
いったいどういうこと?
start_6.png

7.RestControllerをSpringBootApplicationが置いてあるパッケージに移動させてみる
start_7.png

8.再度ブラウザーからアクセス
なぜか、動いた!という事は、SpringApplicationが置いてある配下しかコントローラは作れないってこと?
start_8.png

9.試しにSpringApplicationの配下にパッケージを作ってみます。

TestRest.java
package com.example.demo.controller;

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

@RestController
public class TestRest {

	@RequestMapping("/Rest")
	public String Rest() {
		return "これはRest。demo配下のパッケージより";
	}

}

やっぱりそうか、そうなのね。
start_9.png
まぁともかくちょっと理解が進んだ。
RestControllerはこれくらいでいいだろう。
次に進みます。Controllerを使って、テンプレートエンジンを使ってみます。

10.Controllerを作ってみる。

「templates」フォルダにindex.htmlを作成します。

index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>初めてのThymeleaf</title>
</head>
<body>
 <h1 th:text="${message}"></h1>
</body>
</html>

com.example.demo.controllerに「TestController.java」を作成。

TestController.java
package com.example.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestController {

	@RequestMapping("/")
	public String index(Model model) {
		model.addAttribute("message", "Thymeleaf動きました!");
		return "index";
	}

}

start_10.png

11.実行してtemplatesの動作確認
ちゃんと動きました!めでたしめでたし。
文字コードは何も意識しないで文字化けもせず、すんなりとここまで来れるんですね。
Spring Bootなかなかよさそうですね。次はフォームを作って見よう。
start_11.png

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?