12
12

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.

SpringBoot 概要

Last updated at Posted at 2015-08-23

Spring Boot の概要説明しちゃいます

Spring bootとはSpring4のラッパーでSpringフレームワークを使ったアプリが簡単に作れちゃうフレームワークです。
初期設定なしで初心者でも簡単に導入できます。
STSを使うと更に簡単にデバッグする事ができます。

SpringBoot
http://projects.spring.io/spring-boot/

Spring Boot Reference
http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/

STS
https://spring.io/tools/sts

JAVAライブラリの依存関係解消のために、
mavenとgradleが利用できます。

導入手順
今回はガイドの中から一個だけやってみましょう。
https://spring.io/guides/gs/actuator-service/#initial
上記サンプルを上から順にやるといいです。

1.javacのインストールとパスの設定
2.gitのインストールとパスの設定
3.サンプルダウンロード

git clone https://github.com/spring-guides/gs-actuator-service.git

4.作業用のinitialディレクトリへ移動

cd gs-actuator-service/initial

(complete/にはハンズオン用の解答がはいってます)

5.Run the empty service
@SpringBootApplicationアノテーションを
をメインメソッドが書いてあるクラスにつけます。
メインメソッドの中で自クラスをSpringApplication.runにするだけでサーバーが立ち上がります。

ちなみに、STSのプロジェクトの中ではmainメソッドを何個おいても大丈夫です。実行時に選べます!便利!
maFOhT0vMUqhQx9nW0xS38jRWUznVWRsUs0-BGxlXRQ.png

gradleでコンパイルする際には、下記の記述を追加する事でメインメソッドのクラスを選択します。

build.gradle
springBoot {
  mainClass = "demo.SpringDemoApplication"
}

6.Create a representation class
これはDTOですね。クラスをまるごとコントローラー内でメソッドの戻り値に指定してやる事ができます。
この時に戻り値の前に@ResponseBodyアノテーションを付与します。

public @ResponseBody Greeting sayHello

こうする事でなんと勝手にJSON出力にして返してくれるんですねー。

7.Create a resource controller
@ControllerアノテーションをつけたクラスがWebMVCのフロントコントローラーになります。
デフォルトで起動時に@ComponentScanしてくれてURLと呼び出し先メソッドのマッピングを作成してくれます。

@RequestMapping("/hello-world")
どのURLにどのクラス、どのメソッドをマッピングするかを定義します。

このサンプルの場合は、クラスに"/hello-world"、メソッドに何もついていないので、/hello-worldを呼び出した際にデフォルトでsayHelloが呼ばれています。

sayHello@RequestMappingを次のように書き換えると/hello-world/hogeで呼び出せるはずです。

@RequestMapping(value="hoge",method=RequestMethod.GET)

実装していくにあたって、一点だけ注意する点があって、
SpringBoot自体は主にサーバーとかコンテナといったSpringの実行環境の補助をするものなので、SpringBootに搭載されていない機能を使う場合には、Spring4の実装していく形になります。
やりたい事がある場合にはSpringBootに機能があるかどうか→Spring4に機能があるかどうかの順で考えると分かりやすいです。

12
12
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
12
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?