第一步:新建項目
新建一個SpringBoot工程
修改項目信息
勾選項目依賴和工具
選擇好項目的位置,點擊【Finish】
第二步:項目結構分析
新建好項目之后的結構如下圖所示,少了很多配置文件:
簡單分析下都有啥玩意:
- SpringbootApplication: 一個帶有 main() 方法的類,用於啟動應用程序
- SpringbootApplicationTests:一個空的 Junit 測試了,它加載了一個使用 Spring Boot 字典配置功能的 Spring 應用程序上下文
- application.properties:一個空的 properties 文件,可以根據需要添加配置屬性
- pom.xml: Maven 構建說明文件
第三步:第一個程序試運行
新建一個【HelloController】,並啟動 Spring Boot來查看類運行的結果
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * 測試控制器 * PS:@RestController 注解: 該注解是 @Controller 和 @ResponseBody 注解的合體版 */ @RestController public class TestController { @RequestMapping("/test") public String hello() { return "第一個程序試運行成功!"; } }
回到 SpringbootApplication 這個類中,然后右鍵點擊運行:
運行成功的界面:
可以看到我們的 Tomcat 運行在 8080 端口,我們來訪問 “/test” 地址試一下:
可以看到頁面成功顯示出我們返回的信息。
參考資料:
- Spring Boot【快速入門】(特此感謝!)