spring-boot-starter
When learning Spring Boot, you can understand and practice its basic concepts and functions through a complete case. Here is a simple Spring Boot
Starter complete example showing how to create a basic web application:
First, create a Maven project file named pom.xml, add the following content: idea or other direct creation skip!
<?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.7.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.lfsun</groupId>
<artifactId>springboot-study-2023</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-study-2023</name>
<description>springboot-study-2023</description>
<packaging>pom</packaging>
<properties>
<java.version>1.8</java.version>
<spring-boot.version>2.7.4</spring-boot.version>
</properties>
<dependencies>
<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>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
子模块:
<?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>com.lfsun</groupId>
<artifactId>springboot-study-2023</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-starter</name>
<description>spring-boot-starter</description>
<dependencies>
<!-- Spring Boot Starter Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
In the above example, we defined a Maven project and added the Spring Boot Starter Web dependency, which will provide us with the dependencies needed to build a web-based application.
Next, create a Java class called SpringBootStarterApplication.java with the following content:
package com.lfsun.springbootstarter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootStarterApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootStarterApplication.class, args);
}
}
@RestController
class HelloController {
@GetMapping("/hello")
public String hello() {
return "Hello, World!";
}
}
In this example, we create an
entry class SpringBootStarterApplication for a Spring Boot application
, marked with the @SpringBootApplication annotation. We also created a HelloController class and used @RestController and @GetMapping annotations to handle HTTP
GET requests and return "Hello, World!".
Now, you can build and run the application using a build tool such as Maven. Execute the following command:
[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-ALHAncMp-1685973462761)(img.png)]
mvn clean package
java -jar .\target\spring-boot-starter-0.0.1-SNAPSHOT.jar
The application will start, listening on the default port 8080. You can visit http://localhost:8080/hello in your browser and you will see the "Hello, World!" message returned.