English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this section, we will create and run a simple Spring Boot application.
Steps1: Open Spring Initializr https://start.spring.io/.
Steps2: select the Spring Boot version 222.BUILD-SNAPSHOT.
Steps3: provide groupname. We provide the group name com.w3codebox.
Steps4: provide artifact. We have provided the artifact spring-boot-application-run.
Steps5: add Spring Web dependencies.
Steps6: click Generate button. When we click the "Generate" button, it will package all the specifications related to the application into a Jar the file and download it to the local system.
Steps7: extract jar file.
Steps8: Copy the folder and paste it into the STS workspace.
Steps9: ImportThis project.
File->Import->Existing Maven project->Next->Browse->Select folder spring- spring -boot-application->Run->Select folder->Complete
Importing the project takes time. After successfully importing the project, we can view it in the IDE's Package Explorer section to see it.
We see that two files have been automatically created, one is pom.xml ,另一个是 Application.java files.
pom.xml files contain all dependencies, application name, Spring Boot version, group name, artifact,and other plugins.
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 <modelVersion>4/modelVersion> <parent> <groupId>org.springframework.boot</groupId>/groupId <artifactId>spring</artifactId>-boot-starter-parent>/artifactId <version>222.BUILD-SNAPSHOT/version> <relativePath/>-- lookup parent from repository --> </parent> <groupId>com.w</groupId>3codebox/groupId <artifactId>spring</artifactId>-boot-application-run/artifactId <version>0.0.</version>1-SNAPSHOT/version> <name>spring</name>-boot-application-run/name> <description>Demo project for Spring Boot</description>/description> <properties> <java.version>18</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId>/groupId <artifactId>spring</artifactId>-boot-starter/artifactId </dependency <dependency> <groupId>org.springframework.boot</groupId>/groupId <artifactId>spring</artifactId>-boot-starter-parent>/artifactId <version>221/version> <type>pom</type>/type> </dependency <dependency> <groupId>org.springframework.boot</groupId>/groupId <artifactId>spring</artifactId>-boot-starter-web/artifactId </dependency <dependency> <groupId>org.springframework.boot</groupId>/groupId <artifactId>spring</artifactId>-boot-starter-test/artifactId <scope>test</scope>/scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId>/groupId <artifactId>junit</artifactId>-vintage-engine/artifactId </exclusion </exclusions </dependency </dependencies <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId>/groupId <artifactId>spring</artifactId>-boot-maven-plugin/artifactId </plugin </plugins </build <repositories> <repository> <id>spring</id>-milestones/id> <name>Spring Milestones</name>/name> <url>https://repo.spring.io/milestone/url> </repository <repository> <id>spring</id>-snapshots/id> <name>Spring Snapshots</name>/name> <url>https://repo.spring.io/snapshot/url> <snapshots> <enabled>true</enabled>/enabled> </snapshots> </repository </repositories <pluginRepositories> <pluginRepository> <id>spring</id>-milestones/id> <name>Spring Milestones</name>/name> <url>https://repo.spring.io/milestone/url> </pluginRepository> <pluginRepository> <id>spring</id>-snapshots/id> <name>Spring Snapshots</name>/name> <url>https://repo.spring.io/snapshot/url> <snapshots> <enabled>true</enabled>/enabled> </snapshots> </pluginRepository> </pluginRepositories> </project>
main The class is a class that contains the main() method. It starts the Spring ApplicationContext. This is the class we run to execute the application.
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 displays the message that we have returned to the controller.