Spring Boot集成Web项目
记录一下用 Spring Boot 搭建基础 Web 项目的过程,包含 Maven 配置、代码结构、配置文件、启动测试,以及如何替换默认容器。
Maven 配置
新建一个 Maven 项目,配置 pom.xml。
继承 Spring Boot 父项目,统一管理依赖版本:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.5.0</version>
</parent>
<properties>
<java.version>21</java.version>
</properties>添加 Web 依赖,这一个就够了:
<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>代码
项目结构
web-demo/
├── src/
│ ├── main/
│ │ ├── java/wang/smalleyes/cloud/
│ │ │ ├── WebApplication.java
│ │ │ └── controller/
│ │ │ └── HelloController.java
│ │ └── resources/
│ │ └── application.yml
└── pom.xml启动类放在包根目录,这样组件扫描才能覆盖所有子包。
启动类
package wang.smalleyes.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class WebApplication {
public static void main(String[] args) {
SpringApplication.run(WebApplication.class, args);
}
}控制器
package wang.smalleyes.cloud.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
public class HelloController {
@GetMapping("/hello")
public Map<String, Object> hello() {
Map<String, Object> result = new HashMap<>();
result.put("message", "Hello from Spring Boot!");
result.put("status", "success");
return result;
}
}配置
application.yml 配置基础参数:
server:
port: 8080
spring:
application:
name: web-demo
logging:
level:
root: INFO启动测试
运行 WebApplication.main() 方法,看到日志输出正常启动后,访问接口测试:
curl http://localhost:8080/hello返回结果:
{
"message": "Hello from Spring Boot!",
"status": "success"
}替换默认容器
Spring Boot 默认使用 Tomcat 作为 Web 容器。如果要替换成 Jetty 或 Undertow,修改 Maven 依赖即可。
替换为 Jetty
在 pom.xml 中排除 Tomcat 依赖,添加 Jetty:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
</dependencies>替换为 Undertow
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
</dependencies>启动时可以从日志看到容器类型变化,Tomcat 会显示 "Tomcat started",Jetty 显示 "Jetty started",Undertow 显示 "Undertow started"。
注意事项
- spring-boot-starter-web 包含了 Web 开发需要的所有依赖
- 启动类要放在包根目录,否则组件扫描可能漏掉 Controller
- Spring Boot 内置了 Web 容器,不需要单独安装
- 替换容器只需要修改 Maven 依赖,不需要改配置