1、创建一个Maven项目,并为其导入相关依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.wanxi.boot</groupId>
<artifactId>SpringBootDay01</artifactId>
<version>1.0-SNAPSHOT</version>
<name>SpringBootDay01</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.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<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>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
2、创建一个加上@RestController注解的controller类。
@RestController
public class HelloController
{
@RequestMapping("/hello")
public String hello()
{
return "Hello Spring Boot !";
}
}
3、编写引导类(SpringBoot项目入口),并为其添加@SpringBootApplication注解
@SpringBootApplication:@SpringBootApplication 标注的类为 SpringBoot 的主配置类,Spring Boot 会运行这个类的 main 方法来启动 Spring Boot 应用。
@SpringBootApplication
public class HelloApplication
{
public static void main(String[] args)
{
SpringApplication.run(HelloApplication.class, args);
}
}
4、测试运行结果
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.7.3)
2022-10-24 21:44:19.242 INFO 15032 --- [ main] com.wanxi.boot.HelloApplication : Starting HelloApplication using Java 1.8.0_101 on DESKTOP-V66CLH5 with PID 15032 (C:\D\Wanxi\StageSeven\SpringBoot\SpringBootDay01\target\classes started by 11817 in C:\D\Wanxi\StageSeven\SpringBoot)
2022-10-24 21:44:19.244 INFO 15032 --- [ main] com.wanxi.boot.HelloApplication : No active profile set, falling back to 1 default profile: "default"
2022-10-24 21:44:19.855 INFO 15032 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2022-10-24 21:44:19.855 INFO 15032 --- [ main] o.a.catalina.core.AprLifecycleListener : Loaded Apache Tomcat Native library [1.2.30] using APR version [1.7.0].
2022-10-24 21:44:19.855 INFO 15032 --- [ main] o.a.catalina.core.AprLifecycleListener : APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true], UDS [true].
2022-10-24 21:44:19.855 INFO 15032 --- [ main] o.a.catalina.core.AprLifecycleListener : APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
2022-10-24 21:44:19.862 INFO 15032 --- [ main] o.a.catalina.core.AprLifecycleListener : OpenSSL successfully initialized [OpenSSL 1.1.1k 25 Mar 2021]
2022-10-24 21:44:19.868 INFO 15032 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-10-24 21:44:19.868 INFO 15032 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.65]
2022-10-24 21:44:19.951 INFO 15032 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2022-10-24 21:44:19.951 INFO 15032 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 680 ms
2022-10-24 21:44:20.230 INFO 15032 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2022-10-24 21:44:20.237 INFO 15032 --- [ main] com.wanxi.boot.HelloApplication