springboot簡介:
Spring Boot是Spring社區發布的一個開源項目,旨在幫助開發者快速並且更簡單的構建項目。它使用習慣優於配置的理念讓你的項目快速運行起來,使用Spring Boot很容易創建一個獨立運行(運行jar,內置Servlet容器,Tomcat、jetty)、准生產級別的基於Spring框架的項目,使用SpringBoot你可以不用或者只需要很少的配置文件。
我這里用的是idea創建的如圖:
這里直接選擇spring initializr 其它默認就好,一直點擊下一步就好,這里挺簡單的
如下圖繼續點擊下一步,包名jdk這些自己隨意
如下圖,這里需要選擇依賴,我們就以springweb為例,平常我們也用這個最多
繼續下一步,選擇項目名稱,和項目位置,點擊finish 即可,創建好后,如下圖所示
這里備注一下,各個文件的含義:
java 文件目錄 是java開發的代碼存放目錄, 注意的是 程序的 main 方法的執行類,需要放到其他的被spring掃碼的類的上一級目錄,如果是同級或者更低級目錄,會導致其它類不能被掃描到
resources 項目靜態資源的目錄,springboot 配置靜態資源路徑(默認是/static 、/public 、 /resources 和/META-INF/resources) 開啟自定義配置后,默認不生效
application.yml 配置文件,spring創建好的項目默認是application.properties 文件,我這里是改了后綴用的 yml 文件
寫個controller 執行個hello world 吧!
package com.example.controller; import com.example.exception.ServiceException; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; @Controller @RequestMapping("/home") public class HomeController { @GetMapping("/getHome") private String getHome () { System.out.println("進入getHome"); return "/home"; } }
這里請求轉發到頁面 home ,默認是templates 作為根目錄,當然這里需要配置一下
server: port: 8080 spring: resources: # 配置靜態資源路徑(默認是/static 、/public 、 /resources 和/META-INF/resources) 開啟自定義配置后,默認不生效 static-locations: classpath:templates/ mvc: # mvc 返回頁面路徑時,加上.html (返回頁面路徑就不用加 .html) view: prefix: / suffix: .html
一個helloworld就好了