Do not use start.spring. IO/idea tools, build springboot project from 0, familiar with the project construction process
Environment Preparation:

Maven: 3.3+, Java: 8+, Java environment variables are configured correctly, and pass the Java-version and mvn-version verification

1. Select one of your favorite text tools, create a pom. XML file, and copy the following contents into it to save

<? The XML version = "1.0" encoding = "utf-8"? > < project XMLNS: xsi = "http://www.w3.org/2001/XMLSchema-instance" XMLNS = "http://maven.apache.org/POM/4.0.0" Xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" > The < modelVersion > 4.0.0 < / modelVersion > < groupId > com. Example < / groupId > < artifactId > myproject < / artifactId > < version > 0.0.1 - the SNAPSHOT < / version > < the parent > < groupId > org. Springframework. Boot < / groupId > < artifactId > spring - the boot - starter - parent < / artifactId > < version > 2.4.5 < / version > < / parent > < project >Copy the code

After saving, you can run the MVN Package under the same directory as POM.xml to check whether the BUILD SUCCESS is normal, indicating that the POM file is correct, as shown in the figure below

2. In the poM file above, we introduce the springboot parent project (spring-boot-starter-parent), which contains the basic JAR (Starts) version information. So if the jar to be imported later is already managed by the SpringBoot parent project, there is no need to import version information later. For example, let’s build a Web project this time and add the following dependencies to the POM file

<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId>  </dependency> </dependencies>Copy the code

After introducing web dependencies, you can execute MVN Dependency: Tree to see the JAR dependencies in your project as shown below

3. Use your favorite idea/ Eclipse tool to import the Maven project, create a SRC /main/ Java directory, and create an example.java class

Add the following code

 import org.springframework.boot.*;
 import org.springframework.boot.autoconfigure.*;
 import org.springframework.web.bind.annotation.*;
 @RestController
 @EnableAutoConfiguration
 public class Example {
  @RequestMapping("/")
  String home() {
  return "Hello World!";
  }
  public static void main(String[] args) {
     SpringApplication.run(Example.class, args);
  }
 }
Copy the code
  • Starts can work well with Springboot’s automatic assembly. For example, springboot-starter-Web is added to a project. Springboot will check and start the project as web

  • @restController, @requestMapping is a spring MVC annotation to handle client request routing, @enableAutoConfiguration is a SpringBoot annotation, The auto-assembly used to launch SpringBoot includes the auto-configuration of the web project described above

  • We start the application directly with the SpringApplication class, and pass example. class as the auto-configuration class to the SpringBoot application to complete the auto-assembly

4. Start the application

If you are using ide/ Eclipse you can run the Example main function directly to start the application or the command line MVN spring-boot:run

Visit: http://localhost:8080 output below

5. Package the Springboot project as an executable JAR file

Need to add plug-ins

<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId>  </plugin> </plugins> </build>Copy the code

Execute MVN Package, Jar you can run the project in the target directory by executing Java -jar myproject-0.0.1 -snapshot. jar, as shown in the figure below

Attached: the complete POM.xml

<? The XML version = "1.0" encoding = "utf-8"? > < project XMLNS: xsi = "http://www.w3.org/2001/XMLSchema-instance" XMLNS = "http://maven.apache.org/POM/4.0.0" Xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" > The < modelVersion > 4.0.0 < / modelVersion > < groupId > com. Example < / groupId > < artifactId > myproject < / artifactId > < version > 0.0.1 - the SNAPSHOT < / version > < the parent > < groupId > org. Springframework. Boot < / groupId > <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.5</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>Copy the code