Java Spring Boot 教程 - 帶有詳細步驟的 Live Hello-World Web 應用程序示例
已發表: 2018-04-05
為什麼選擇 Spring Boot? Spring Boot 是用來做什麼的?
想為您的應用程序創建基於 java 的微服務嗎? 好吧,Spring boot 是一個基於 Java 的開源解決方案
它用於構建由 Pivotal 團隊創建的基於 Spring 的生產就緒應用程序。
Spring Boot 的想法很簡單。 它提供了一組預配置的功能和框架,您只需few lines of code
即可發布您的第一個基於 Spring Boot 的 Web 應用程序。 只需一個簡單的命令,您就可以啟動和測試您的 Web 應用程序。
所有自定義配置、數據庫設置、第 3 方模塊的集合在 Spring Boot 中都是out-0f-the-box
的。
就像我們有非常流行的 Hello World Spring MVC 教程一樣,我們也會以類似的方式開始我們的Hello World Spring Boot
教程。
Spring Boot 入門:
- 這是您的 Spring Boot 初學者教程
- Hello World Spring Boot 教程
- 最簡單的 Spring Boot 示例
- Spring Boot Maven 教程
- Spring Boot Web 應用程序示例
讓我們開始吧。
第1步
為了開始使用 Spring Boot,您將需要以下設置。
- JDK 8. 確保
download and install
最新的 JDK。 - Eclipse IDE。 確保下載
Eclipse IDE for Java EE Developers
。 官方下載鏈接。 - 春季啟動 2.4.3。
第2步
在筆記本電腦/台式機上安裝 Eclipse 後,轉到 Eclipse。 我們將創建新的動態 Web 項目。
- 點擊文件
- 新的
Dynamic Web Project
- 在第一個屏幕上提供項目名稱:
CrunchifySpringBootHelloWorld

- 確保您擁有
3.1
版的動態 Web 模塊版本和目標運行時 Apache Tomcat 8。 - 如果您還沒有 Apache Tomcat 設置,請按照以下步驟在 Eclipse 中設置 Tomcat。
- 單擊下一步兩次並確保設置與這些設置相同。 截圖1,截圖2。
- 或單擊完成以創建動態 Web 項目。
第三步
下一步是將 Java 項目轉換為 Maven 項目。
- 右鍵單擊項目
- 點擊配置
- 單擊
Convert to Maven Project
- 保持默認配置
- 點擊完成

第四步
上述步驟將為您的項目創建 pom.xml 文件。 下一步是使用以下內容更新pom.xml
文件並添加Spring Boot Dependency
。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
< project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns : xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi : schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion > 4.0.0 < / modelVersion > < groupId > CrunchifySpringBootHelloWorld < / groupId > < artifactId > CrunchifySpringBootHelloWorld < / artifactId > < version > 1.0.0 - RELEASE < / version > < packaging > war < / packaging > < parent > < groupId > org . springframework . boot < / groupId > < artifactId > spring - boot - starter - parent < / artifactId > < version > 2.4.3 < / version > < / parent > < dependencies > < dependency > < groupId > org . springframework . boot < / groupId > < artifactId > spring - boot - starter - web < / artifactId > < / dependency > < / dependencies > < properties > < java . version > 1.8 < / java . version > < / properties > < build > < sourceDirectory > src < / sourceDirectory > < plugins > < plugin > < artifactId > maven - compiler - plugin < / artifactId > < configuration > < source > 1.8 < / source > < target > 1.8 < / target > < / configuration > < / plugin > < plugin > < groupId > org . springframework . boot < / groupId > < artifactId > spring - boot - maven - plugin < / artifactId > < / plugin > < plugin > < artifactId > maven - war - plugin < / artifactId > < configuration > < warSourceDirectory > WebContent < / warSourceDirectory > < / configuration > < / plugin > < / plugins > < / build > < / project > |
正如您在上面看到的,我們正在將org.springframework.boot 2.4.3
jar 添加到我們的項目中。 此外,我們還添加了兩個工件:
- spring-boot-maven 插件
- spring-boot-starter-web
第 5 步
現在讓我們創建簡單的Hello World Application
文件,我們將能夠使用 URL 訪問它
- http://locahost:8080
- http://locahost:8080/crunchify
使用以下代碼內容創建文件CrunchifyHelloWorldSpringBoot.java
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package crunchify . com . springboot ; import org . springframework . boot . SpringApplication ; import org . springframework . boot . autoconfigure . EnableAutoConfiguration ; import org . springframework . web . bind . annotation . RequestMapping ; import org . springframework . web . bind . annotation . RestController ; /** * @author Crunchify.com - Simple Spring Boot Example * */ // @RestController is a convenience annotation that is itself annotated with @Controller and @ResponseBody @ RestController // @EnableAutoConfiguration enables auto-configuration of the Spring Application Context, attempting to guess // and configure beans that you are likely to need. @ EnableAutoConfiguration public class CrunchifyHelloWorldSpringBoot { // @RequestMapping annotation is used for mapping web requests onto specific handler classes @ RequestMapping ( "/" ) String basicURL ( ) { return "Welcome to Spring Boot Hello World Tutorial" ; } @ RequestMapping ( "crunchify" ) String crunchifyURL ( ) { return "Hello Crunchify Friends! This is your first SpringBoot Example. Isn't that so Simple?" ; } public static void main ( String [ ] args ) throws Exception { // SpringApplication classes that can be used to bootstrap and launch a Spring application from a Java // main method. By default class will perform the following steps to bootstrap your application. SpringApplication . run ( CrunchifyHelloWorldSpringBoot . class , args ) ; } } |
添加上述 java 文件後,您的 Eclipse 項目結構應如下所示:

第 6 步
現在使用簡單spring-boot:run
maven 命令,您可以啟動您的Spring Boot Application
。
- 右鍵單擊項目
- 單擊
Run As -> Maven build...

接下來的 Windows 保持默認配置並提供goal as spring-boot:run
,如下圖所示。

第 7 步
確保在 Eclipse 控制台中沒有任何錯誤。 這是 Eclipse IDE 的控制台輸出。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
SLF4J : Class path contains multiple SLF4J bindings . SLF4J : Found binding in [ jar : file : / Applications / Eclipse . app / Contents / Eclipse / plugins / org . eclipse . m2e . maven . runtime . slf4j . simple_1 . 16.0.20200610 - 1735 / jars / slf4j - simple - 1.7.5.jar ! / org / slf4j / impl / StaticLoggerBinder . class ] SLF4J : Found binding in [ file : / Applications / Eclipse . app / Contents / Eclipse / configuration / org . eclipse . osgi / 5 / 0 / . cp / org / slf4j / impl / StaticLoggerBinder . class ] SLF4J : See http : //www.slf4j.org/codes.html#multiple_bindings for an explanation. SLF4J : Actual binding is of type [ org . slf4j . impl . SimpleLoggerFactory ] SLF4J : Class path contains multiple SLF4J bindings . SLF4J : Found binding in [ jar : file : / Applications / Eclipse . app / Contents / Eclipse / plugins / org . eclipse . m2e . maven . runtime . slf4j . simple_1 . 16.0.20200610 - 1735 / jars / slf4j - simple - 1.7.5.jar ! / org / slf4j / impl / StaticLoggerBinder . class ] SLF4J : Found binding in [ file : / Applications / Eclipse . app / Contents / Eclipse / configuration / org . eclipse . osgi / 5 / 0 / . cp / org / slf4j / impl / StaticLoggerBinder . class ] SLF4J : See http : //www.slf4j.org/codes.html#multiple_bindings for an explanation. SLF4J : Actual binding is of type [ org . slf4j . impl . SimpleLoggerFactory ] [ INFO ] Scanning for projects . . . [ INFO ] [ INFO ] ---- < CrunchifySpringBootHelloWorld : CrunchifySpringBootHelloWorld > ----- [ INFO ] Building CrunchifySpringBootHelloWorld 1.0.0 - RELEASE [ INFO ] -------------------------------- [ war ] --------------------------------- [ INFO ] [ INFO ] > > > spring - boot - maven - plugin : 2.4.3 : run ( default - cli ) > test - compile @ CrunchifySpringBootHelloWorld > > > [ INFO ] [ INFO ] --- maven - resources - plugin : 3.2.0 : resources ( default - resources ) @ CrunchifySpringBootHelloWorld --- [ INFO ] Using 'UTF-8' encoding to copy filtered resources . [ INFO ] Using 'UTF-8' encoding to copy filtered properties files . [ INFO ] skip non existing resourceDirectory / Users / app / crunchify / eclipse - workspace / CrunchifySpringBootHelloWorld / src / main / resources [ INFO ] skip non existing resourceDirectory / Users / app / crunchify / eclipse - workspace / CrunchifySpringBootHelloWorld / src / main / resources [ INFO ] [ INFO ] --- maven - compiler - plugin : 3.8.1 : compile ( default - compile ) @ CrunchifySpringBootHelloWorld --- [ INFO ] Nothing to compile - all classes are up to date [ INFO ] [ INFO ] --- maven - resources - plugin : 3.2.0 : testResources ( default - testResources ) @ CrunchifySpringBootHelloWorld --- [ INFO ] Not copying test resources [ INFO ] [ INFO ] --- maven - compiler - plugin : 3.8.1 : testCompile ( default - testCompile ) @ CrunchifySpringBootHelloWorld --- [ INFO ] Not compiling test sources [ INFO ] [ INFO ] < < < spring - boot - maven - plugin : 2.4.3 : run ( default - cli ) < test - compile @ CrunchifySpringBootHelloWorld < < < [ INFO ] [ INFO ] [ INFO ] --- spring - boot - maven - plugin : 2.4.3 : run ( default - cli ) @ CrunchifySpringBootHelloWorld --- [ INFO ] Attaching agents : [ ] . ____ _ __ _ _ / \ \ / ___ '_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | ' _ | '_| | ' _ \ / _ ` | \ \ \ \ \ \ / ___ ) | | _ ) | | | | | | | ( _ | | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.4.3) 2021-02-21 21:05:10.614 INFO 36654 --- [ main] c.c.s.CrunchifyHelloWorldSpringBoot : Starting CrunchifyHelloWorldSpringBoot using Java 15 on LM-AUN-11021095 with PID 36654 (/Users/app/crunchify/eclipse-workspace/CrunchifySpringBootHelloWorld/target/classes started by arpshah in /Users/app/crunchify/eclipse-workspace/CrunchifySpringBootHelloWorld) 2021-02-21 21:05:10.616 INFO 36654 --- [ main] c.c.s.CrunchifyHelloWorldSpringBoot : No active profile set, falling back to default profiles: default 2021-02-21 21:05:11.008 INFO 36654 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2021-02-21 21:05:11.014 INFO 36654 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2021-02-21 21:05:11.015 INFO 36654 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.43] 2021-02-21 21:05:11.054 INFO 36654 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2021-02-21 21:05:11.054 INFO 36654 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 417 ms 2021-02-21 21:05:11.147 INFO 36654 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService ' applicationTaskExecutor ' 2021-02-21 21:05:11.241 INFO 36654 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ' ' 2021-02-21 21:05:11.247 INFO 36654 --- [ main] c.c.s.CrunchifyHelloWorldSpringBoot : Started CrunchifyHelloWorldSpringBoot in 0.843 seconds (JVM running for 1.04) 2021-02-21 21:07:04.240 INFO 36654 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet ' dispatcherServlet ' 2021-02-21 21:07:04.240 INFO 36654 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet ' dispatcherServlet ' 2021 - 02 - 21 21 : 07 : 04.241 INFO 36654 --- [ nio - 8080 - exec - 1 ] o . s . web . servlet . DispatcherServlet : Completed initialization in 1 ms |
步驟 8
現在訪問 URL http://localhost:8080
和http://locahost:8080/crunchify
來查看實時結果。



希望本教程可以幫助您開始使用 Java Spring Boot 示例。 如果您在運行您的第一個 Spring Boot 應用程序時發現任何問題,請告訴我。
在接下來的幾週內,我們將為您添加越來越複雜的 Spring Boot 示例。 如果您有任何特殊要求,請在評論部分告訴我們。
接下來是什麼?
- 在 IntelliJ IDEA 中創建超級簡單的
Spring Boot application
- 在 IntelliJ IDEA 中創建簡單的 Web
Rest Service