English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In diesem Abschnitt erstellen und ausführen wir eine einfache Spring Boot-Anwendung.
Steps1: Öffnen Sie Spring Initializr https://start.spring.io/.
Steps2: Wählen Sie die Version von Spring Boot aus 2.2.2.BUILD-SNAPSHOT.
Steps3: Bereitstellen GruppeName. Wir haben den Gruppennamen bereitgestellt com.w3codebox.
Steps4: Bereitstellen ArtefakteWir haben die Artefakte bereitgestellt spring-boot-application-run.
Steps5: Hinzufügen Spring Web Abhängigkeiten.
Steps6: Klicken Generate Kнопfe. Wenn wir auf die Schaltfläche "Generieren" klicken, packt sie alle Standards mit der Anwendung in eine Jar Datei und laden Sie sie auf das lokale System herunter.
Steps7: Extrahieren Jar-Datei.
Steps8: 复制文件夹并将其粘贴到STS工作区中。
Steps9: 导入该项目。
文件->导入->现有Maven项目->下一步->浏览->选择文件夹spring- spring -boot-application-run->选择文件夹->完成
导入项目需要时间。成功导入项目后,我们可以在IDE的 Package Explorer 部分中看到它。
我们看到自动创建了两个文件,一个是 pom.xml ,另一个是 Application.java 文件。
pom.xml 文件包含所有 依赖项, 应用程序名称,Spring引导版本,组名,工件,和其他 插件。
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.w3codebox</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 Die Klasse ist eine Klasse, die den Methoden main() enthält. Sie startet die Spring ApplicationContext. Dies ist die Klasse, die wir ausführen, um die Anwendung auszuführen.
SpringBootApplicationRun.java
package com.w3codebox; 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); } }
Steps10: Create a controller. We created a controller named HelloWorldController Controller.
HelloWorldController.java
package com.w3codebox; 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"; } }
Now, we have created all the necessary files related to the Spring Boot application.
To run the Spring Boot application, open the main application file and then run it with Run it as a Java Application.
When the application runs successfully, it will display the message in the console as follows.
Now, open the browser and call the URL http://localhost:8080. It shows that we have returned the message to the controller.