在本節(jié)中,我們將創(chuàng)建并運(yùn)行一個(gè)簡(jiǎn)單的Spring Boot應(yīng)用程序。
步驟1: 打開(kāi)Spring Initializr https://start.spring.io/。
步驟2: 選擇 Spring Boot版本 2.2.2.BUILD-SNAPSHOT。
步驟3: 提供 組名稱(chēng)。我們提供了組名 com.nhooo。
步驟4: 提供 工件。我們已經(jīng)提供了工件 spring-boot-application-run。
步驟5: 添加 Spring Web 依賴(lài)項(xiàng)。
步驟6: 單擊 Generate 按鈕。當(dāng)我們單擊"生成"按鈕時(shí),它將與應(yīng)用程序相關(guān)的所有規(guī)范包裝到一個(gè) Jar 文件中,并將其下載到本地系統(tǒng)。
步驟7: 提取 jar文件。
步驟8: 復(fù)制文件夾并將其粘貼到STS工作區(qū)中。
步驟9: 導(dǎo)入該項(xiàng)目。
文件->導(dǎo)入->現(xiàn)有Maven項(xiàng)目->下一步->瀏覽->選擇文件夾spring- spring -boot-application-run->選擇文件夾->完成
導(dǎo)入項(xiàng)目需要時(shí)間。成功導(dǎo)入項(xiàng)目后,我們可以在IDE的 Package Explorer 部分中看到它。
我們看到自動(dòng)創(chuàng)建了兩個(gè)文件,一個(gè)是 pom.xml ,另一個(gè)是 Application.java 文件。
pom.xml 文件包含所有 依賴(lài)項(xiàng), 應(yīng)用程序名稱(chē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 類(lèi)是一個(gè)包含main()方法的類(lèi)。它啟動(dòng)Spring ApplicationContext。這是我們?yōu)閳?zhí)行應(yīng)用程序而運(yùn)行的類(lèi)。
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)建一個(gè)控制器。我們創(chuàng)建了一個(gè)名稱(chē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)的所有必需文件。
用于運(yùn)行 Spring Boot應(yīng)用程序 ,打開(kāi)主應(yīng)用程序文件,然后以 Java Application的身份運(yùn)行它。
當(dāng)應(yīng)用程序成功運(yùn)行時(shí),它會(huì)在控制臺(tái)中顯示該消息,如下所示。
現(xiàn)在,打開(kāi)瀏覽器并調(diào)用URL http://localhost:8080。它顯示了我們已返回控制器的消息。