SpringBoot入門(STS V4確認版)


X1.はじめてのSpringBoot(STS V4動作確認)

X1-1 HelloWorld!

④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.RequestMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RestController public class DemoApplication { @RequestMapping("/") String home() { return "Hello World!"; } public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }

@RestControllerアノテーション @RestControllerを記述することで、このクラスがWEBアプリケーションとして 操作できるコントローラプログラムとして動作できるようになります。

@RequestMappingアノテーション @RequestMappingは、リクエストを受けたときの処理を記述します。 このプログラムの場合、"/"(ルート)にアクセスが来た場合の処理です。 例) @RequestMapping("/") → http://localhost:8080/でアクセスされた場合 @RequestMapping("/user") → http://localhost:8080/user/でアクセスされた場合