1、Spring Boot 是什麼css
2、爲何要使用 Spring Boothtml
3、快速入門java
4、總結git
5、GitHub 示例代碼github
如下截圖自 Spring Boot 官方文檔:web
翻譯整理一下,內容以下:面試
Spring Boot 是基於 Spring 框架基礎上推出的一個全新的框架, 旨在讓開發者能夠輕鬆地建立一個可獨立運行的,生產級別的應用程序。spring
基於 Spring Boot 內部的自動化配置功能,開發者能夠在"零"配置, 或者只須要添加不多的配置,就能夠進行平常的功能開發。shell
用權威說話,下圖一樣截圖自 Spring Boot 官方文檔:apache
咱們再來翻譯整理一下,內容以下:
怎麼樣,看了 Spring Boot 這些強大的特性之後,是否是開始躍躍欲試了,接下來,讓咱們快速入門 Spring Boot 吧 !
PS: 正因爲 Spring Boot 以上特性,才得以使它成爲構建微服務架構的基礎組件。
默認狀況下,Spring Initializr 生成的項目是經過 Maven 來構建的,開發語言爲 Java, 版本用的最新的發行版,打包方式爲 Jar, 使用的 Java 版本爲 1.8,小夥伴們這裏要注意一下!
點擊 Generate Project
按鈕,下載的 Demo.zip
, 解壓後,導入到開發工具中,這裏筆者使用的是 IntelliJ IDEA。
至此,經過 Spring Initializr 網站來建立應用,並導入到了咱們的開發工具 IntelliJ IDEA 中就成功了。
PS: 若是您使用的是 Eclipse, 方式是 Import -> Existing Maven Projects -> Next -> 選擇解壓後的文件夾 -> Finsh
其實,咱們還能夠直接經過 IntelliJ IDEA 來建立一個 Spring Boot 項目,由於 IntelliJ IDEA 內置了 Spring Initializr,接下來,經過圖文,讓咱們來看下要如何一步一步操做:
項目建立成功後,能夠看到結構以下:
共分爲三個主要的文件夾:
src/mail/java
: 存放 Java 源碼,包括啓動程序的入口;src/mail/resources
: 資源目錄,用於放置相關配置文件,靜態文件 html, css 等;src/test/java
: 存放單元測試類在 pom.xml
文件中添加 web
依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
PS: 不用手動去指定版本號,由於 Spring Boot 內部已經維護相關 Jar 包的依賴關係。
在 com.example.demo
包下添加 controller
包,用來存放全部對外部開發的接口, 完成後,建立 HelloController.java
類, 添加一個 /hello
接口:
@RestController public class HelloController { @GetMapping("/hello") public String hello() { return "Hello, Spring Boot !"; } }
添加 @RestController
註解,表示此類中全部定義的接口均爲 RESTFul 風格,也就是說返參均爲 JSON 格式的。
@GetMapping("/hello")
表示定義一個 GET 請求的接口,路徑爲 /hello
。
根據圖示,點擊按鈕,來啓動 Spring Boot Web 程序, 查看控制檯輸出:
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.1.4.RELEASE) 2019-04-14 19:57:59.795 INFO 3680 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication on 192.168.0.100 with PID 3680 (/Users/a123123/Work/IdeaProjects/demo/target/classes started by allen-jiang in /Users/a123123/Work/IdeaProjects/demo) 2019-04-14 19:57:59.798 INFO 3680 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default 2019-04-14 19:58:00.666 INFO 3680 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2019-04-14 19:58:00.689 INFO 3680 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2019-04-14 19:58:00.689 INFO 3680 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.17] 2019-04-14 19:58:00.756 INFO 3680 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2019-04-14 19:58:00.757 INFO 3680 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 920 ms 2019-04-14 19:58:00.978 INFO 3680 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2019-04-14 19:58:01.183 INFO 3680 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2019-04-14 19:58:01.186 INFO 3680 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 1.678 seconds (JVM running for 2.176)
當控制檯日誌中輸出了 Tomcat started on port(s): 8080 (http) with context path ''
時,表示內嵌的 Tomcat 容器已經啓動成功了,端口爲 8080 !
接下來,咱們在瀏覽器上訪問 http://localhost:8080/hello
接口,驗證一下,接口是否可以正常訪問:
返回了咱們想要的 Hello, Spring Boot !
字符串,接口訪問正常,大工告成!
本文中,咱們首先了解了什麼是 Spring Boot, 以及Spring Boot 的相關特性,最後咱們手把手學習瞭如何搭建一個 Spring Boot 項目,並編寫了第一個接口且訪問成功。
經過實際操做,真切地感覺到了 Spring Boot 的魅力所在!老鐵,雙擊 666!
https://github.com/weiwosuoai/spring-boot-tutorial/tree/master/demo
最近在網上發現一個不錯的 PDF 資源《Java 核心面試知識.pdf》分享給你們,不光是面試,學習,你都值得擁有!!!
獲取方式: 關注公衆號: 小哈學Java, 後臺回覆 資源,既可獲取資源連接,下面是目錄以及部分截圖:
重要的事情說兩遍,獲取方式: 關注公衆號: 小哈學Java, 後臺回覆 資源,既可獲取資源連接 !!!
原文出處:https://www.cnblogs.com/quanxiaoha/p/10706850.html