Spring Boot 本地开发指南
使用 Spring Boot 在 localhost:8080 运行 Java Web 应用与 REST API,支持热重载 devtools。
Spring Boot 是 Java 生态最流行的 Web 与微服务框架,内嵌 Tomcat/Jetty。本地运行后默认 **http://localhost:8080**。
快速开始
使用 Spring Initializr 生成项目,或:
# 需 JDK 17+ 与 Maven/Gradle
./mvnw spring-boot:run
# 或
./gradlew bootRun修改端口
src/main/resources/application.properties:
server.port=8081或 application.yml:
server:
port: 8081常用 localhost 路径
| 路径 | 说明 |
|---|---|
/ | 首页或 API 根 |
/actuator/health | 健康检查(需引入 actuator) |
/h2-console | 嵌入式 H2 控制台(若启用) |
本地数据库
开发常用 H2 内存库或连接本地 MySQL/PostgreSQL:
spring.datasource.url=jdbc:postgresql://localhost:5432/mydb
spring.datasource.username=postgres
spring.datasource.password=secretDevTools 热重载
引入 spring-boot-devtools 后,代码变更自动重启(类路径内资源)。
常见问题
8080 被 Tomcat/其他 Java 占用
改 server.port 或停止其他服务。
构建慢
首次下载依赖较慢;可用国内 Maven 镜像。
小结
Spring Boot 本地默认 **http://localhost:8080**,是 Java 后端与 REST API 的标准 localhost 入口。