在本節(jié)中,我們將創(chuàng)建并運行一個簡單的Spring Boot應(yīng)用程序。
步驟1: 打開Spring Initializr https://start.spring.io/。
步驟2: 選擇 Spring Boot版本 2.2.2.BUILD-SNAPSHOT。
步驟3: 提供 組名稱。我們提供了組名 com.nhooo。
步驟4: 提供 工件。我們已經(jīng)提供了工件 spring-boot-application-run。
步驟5: 添加 Spring Web 依賴項。
步驟6: 單擊 Generate 按鈕。當(dāng)我們單擊"生成"按鈕時,它將與應(yīng)用程序相關(guān)的所有規(guī)范包裝到一個 Jar 文件中,并將其下載到本地系統(tǒng)。
步驟7: 提取 jar文件。
步驟8: 復(fù)制文件夾并將其粘貼到STS工作區(qū)中。
步驟9: 導(dǎo)入該項目。
文件->導(dǎo)入->現(xiàn)有Maven項目->下一步->瀏覽->選擇文件夾spring- spring -boot-application-run->選擇文件夾->完成
導(dǎo)入項目需要時間。成功導(dǎo)入項目后,我們可以在IDE的 Package Explorer 部分中看到它。
我們看到自動創(chuàng)建了兩個文件,一個是 pom.xml ,另一個是 Application.java 文件。
pom.xml 文件包含所有 依賴項, 應(yīng)用程序名稱,Spring引導(dǎo)版本,組名,工件,和其他 插件。
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.2.BUILD-SNAPSHOT</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.nhooo</groupId> <artifactId>spring-boot-application-run</artifactId> <version>0.0.1-SNAPSHOT</version> <name>spring-boot-application-run</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.1.RELEASE</version> <type>pom</type> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> </repository> <repository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> </pluginRepository> <pluginRepository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </pluginRepository> </pluginRepositories> </project>
main 類是一個包含main()方法的類。它啟動Spring ApplicationContext。這是我們?yōu)閳?zhí)行應(yīng)用程序而運行的類。
SpringBootApplicationRun.java
package com.nhooo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringBootApplicationRun { public static void main(String[] args) { SpringApplication.run(SpringBootApplicationRun.class, args); } }
步驟10: 創(chuàng)建一個控制器。我們創(chuàng)建了一個名稱為 HelloWorldController 的控制器。
HelloWorldController.java
package com.nhooo; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloWorldController { @RequestMapping("/") public String hello() { return "Hello User"; } }
現(xiàn)在,我們已經(jīng)創(chuàng)建了與 Spring Boot 應(yīng)用程序相關(guān)的所有必需文件。
用于運行 Spring Boot應(yīng)用程序 ,打開主應(yīng)用程序文件,然后以 Java Application的身份運行它。
當(dāng)應(yīng)用程序成功運行時,它會在控制臺中顯示該消息,如下所示。
現(xiàn)在,打開瀏覽器并調(diào)用URL http://localhost:8080。它顯示了我們已返回控制器的消息。