3 分钟创建你的第一个后端应用 —— 真的只要 3 分钟
手动写 XML 配置手动配 Tomcat手动管理依赖版本配完才能写业务自动配置好一切Starter 一键引入写完代码直接运行内嵌 Tomcat,一个 JAR 搞定Spring Boot 帮你做了 95% 的配置工作。你只需要关注那 5% —— 你的业务代码。
starter-web 就包含了 Spring MVC、Tomcat、JSON 处理。不用一个个引入。java -jar 一行命令就能运行。application.yml 几行搞定所有配置。| 对比项 | 传统 Spring | Spring Boot |
|---|---|---|
| 配置方式 | 大量 XML 或 Java 配置 | 自动配置,几乎零配置 |
| 依赖管理 | 手动管理每个版本号 | Starter 统一管理 |
| 服务器 | 需要安装外部 Tomcat | 内嵌 Tomcat,直接运行 |
| 部署方式 | WAR 包部署到 Tomcat | JAR 包独立运行 |
| 上手时间 | 1天+(配环境就半天) | 3分钟(真的!) |
按照下面的步骤,一步一步来。复制粘贴就能运行!
选择 Spring Initializr,填写:
com.examplespringboot-demo💡 也可以在 start.spring.io 网页上生成后下载
IDEA 自动生成了以下文件:
打开根目录的 pom.xml,关键部分如下:
<!-- 📄 pom.xml — Spring Boot 项目依赖配置 -->
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<!-- ⭐ 继承 Spring Boot 父项目: 统一管理所有依赖版本,不用自己写 version -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.0</version>
</parent>
<groupId>com.example</groupId>
<artifactId>springboot-demo</artifactId>
<version>1.0.0</version>
<properties>
<java.version>1.8</java.version> <!-- 指定 Java 版本 -->
</properties>
<dependencies>
<!-- Web 起步依赖: 一个 starter 就包含了 Spring MVC + 内嵌 Tomcat + JSON 处理 -->
<!-- 不需要单独引入这些,starter 帮你全搞定! -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 注意: 不用写 version!父项目已经帮你管理了 -->
</dependency>
<!-- 测试起步依赖: 包含 JUnit5 + MockMvc + 断言库 -->
<!-- scope=test: 只在测试时使用,不会打包到生产环境 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Spring Boot Maven 插件: 打包成可执行的 JAR 文件 -->
<!-- 打包后可以直接 java -jar xxx.jar 运行 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>打开 Application.java,只有几行代码:
// 📄 Application.java — Spring Boot 主启动类(整个项目的入口!)
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
// @SpringBootApplication 是一个组合注解,等于以下三个注解之和:
// ① @SpringBootConfiguration → 标记为配置类(相当于 @Configuration)
// ② @EnableAutoConfiguration → 开启自动配置(Spring Boot 的核心魔法!)
// ③ @ComponentScan → 扫描当前包及子包下的所有组件(@Controller/@Service 等)
@SpringBootApplication
public class Application {
public static void main(String[] args) {
// 这一行就完成了:创建 IoC 容器 + 启动内嵌 Tomcat + 自动配置一切
// 传统 Spring 需要大量 XML 才能做到的事情,这里一行搞定!
SpringApplication.run(Application.class, args);
}
}在 com.example 包下新建 controller 包,然后创建 HelloController.java:
// 📄 HelloController.java — 你的第一个 REST 控制器
package com.example.controller;
import org.springframework.web.bind.annotation.*;
// @RestController = @Controller + @ResponseBody
// 所有方法的返回值直接作为 HTTP 响应体(自动转 JSON)
@RestController
@RequestMapping("/api") // 所有接口以 /api 开头
public class HelloController {
// GET http://localhost:8080/api/hello → 返回字符串
@GetMapping("/hello")
public String hello() {
return "Hello, Spring Boot!";
}
// GET http://localhost:8080/api/hello/张三 → 路径变量
// @PathVariable: 从 URL 路径中取值(/hello/{name} 中的 name)
@GetMapping("/hello/{name}")
public String helloName(@PathVariable String name) {
return "Hello, " + name + "!";
}
// GET http://localhost:8080/api/greet?name=李四 → 查询参数
// @RequestParam: 从 URL 查询参数中取值(?name=xxx)
@GetMapping("/greet")
public String greet(@RequestParam String name) {
return "你好, " + name + "!";
}
// POST http://localhost:8080/api/user → 接收 JSON 请求体
// @RequestBody: 把请求体中的 JSON 自动转为 Java 对象
@PostMapping("/user")
public User createUser(@RequestBody User user) {
user.setId(1L); // 模拟数据库自增 ID
return user; // 返回对象 → Spring 自动转为 JSON 响应
}
}
// 📄 User.java — 用户实体类(可以单独一个文件,这里放一起方便展示)
class User {
private Long id;
private String name;
private Integer age;
// getter/setter(Spring 通过这些方法完成 JSON ↔ 对象的转换)
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public Integer getAge() { return age; }
public void setAge(Integer age) { this.age = age; }
}右键 Application.java → Run 'Application.main()'
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.7.0)
Started Application in 2.345 seconds ← 启动成功!在浏览器地址栏输入:http://localhost:8080/api/hello
看到 "Hello, Spring Boot!" 就大功告成!🎊
点击按钮模拟向你刚创建的 Controller 发送不同的 HTTP 请求:
Spring Boot 的配置文件在 src/main/resources/ 下,支持两种格式:
# 📄 application.properties — 扁平格式
# 服务器端口(默认 8080,可以改成其他端口)
server.port=8080
# 应用名称(用于日志、监控等标识)
spring.application.name=springboot-demo
# 日志级别: root 是全局级别,com.example 是你自己代码的级别
logging.level.root=INFO
logging.level.com.example=DEBUG# 📄 application.yml — 层级格式(推荐!更清晰)
# 服务器配置
server:
port: 8080 # 端口号
# Spring 配置
spring:
application:
name: springboot-demo # 应用名称
# 日志配置
logging:
level:
root: INFO # 全局日志级别
com.example: DEBUG # 自己代码的日志级别(更详细).properties 和 .yml 只需要保留一个。推荐使用 .yml,层级更清晰。如果两个都存在,.properties 优先级更高。
需要什么功能,就在 pom.xml 加对应的 starter,Spring Boot 会自动配置好一切:
| Starter | 功能 | 自动帮你做了什么 |
|---|---|---|
spring-boot-starter-web | Web 开发 | 配好 Spring MVC + Tomcat + JSON |
spring-boot-starter-data-jpa | 数据库 (JPA) | 配好数据源 + 事务 + EntityManager |
spring-boot-starter-data-redis | Redis 缓存 | 配好 Redis 连接 + RedisTemplate |
spring-boot-starter-test | 测试 | 配好 JUnit5 + MockMvc + 断言库 |
spring-boot-starter-actuator | 监控 | 配好健康检查 + 指标 + 端点 |