Spring Boot 3 要啟用 MVC ,需要額外在 pom.xml 中設定需要的 dependency 。
spring-boot-starter-web
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Spring Boot 3 建立 MVC 第一個 Controller
建立一個 package: com.myboot.demo.action

在此 package 中 ,建立一個 HiAction.java

在 class 上方,增加 @RestController 的宣告。

增加 hi 的 method

在 hi 的 method 上方增加 @GetMapping(“/Hi”)

執行上個例子中的 DemoApplication。
可以看到 Tomcat 成功啟動於 8080

開啟瀏覽器,執行 http://localhost:8080/Hi

成功了! 灑花
Spring Boot 3 也可以一開始的時候就把 web 加上去

這樣下載下來的專案結構就會有 web MVC 的功能了
Spring Boot 3 產生 Json 格式資料
增加一個 hi2() 的 method ,並宣告 @GetMapping(“/Hi2”)

開啟瀏覽器,執行 http://localhost:8080/Hi2

輕鬆得到 Json 的回覆。