第 1 课:从零开始
本课目标:先把第一个 Spring Boot 项目跑起来,并理解最核心的几个注解。
- 环境准备
推荐环境:
· JDK 17 或以上
· Maven 3.6+
· IDEA / Eclipse / VS Code 都可以
· Spring Boot 3.x
检查命令:
bash
java -version
mvn -v
注意:Spring Boot 3 要求 Java 17+。如果你只有 JDK 8,可以用 Spring Boot 2.7.x。
- 创建项目
最简单的方式是打开:
text
https://start.spring.io
选择:
· Project:Maven
· Language:Java
· Spring Boot:3.3.x
· Group:com.example
· Artifact:demo
· Packaging:Jar
· Java:17
· Dependencies:添加 Spring Web
点击 Generate,下载解压,然后用 IDEA 打开。
项目结构大致如下:
text
demo
├── pom.xml
└── src
├── main
│ ├── java
│ │ └── com/example/demo
│ │ ├── DemoApplication.java
│ │ └── controller
│ │ └── HelloController.java
│ └── resources
│ └── application.properties
└── test
└── java/com/example/demo/DemoApplicationTests.java
- 核心依赖:pom.xml
关键部分如下:
xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.5</version>
<relativePath/>
</parent>
<properties>
<java.version>17</java.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>
spring-boot-starter-web 会自动引入 Spring MVC、内嵌 Tomcat、JSON 转换等。
- 启动类
文件:src/main/java/com/example/demo/DemoApplication.java
java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
@SpringBootApplication 是 Spring Boot 最核心的注解,它包含:
· @SpringBootConfiguration
· @EnableAutoConfiguration
· @ComponentScan
所以,启动类要放在最外层包,Controller 要放在它的同级包或子包中。
- 第一个接口:Hello World
新建文件:
src/main/java/com/example/demo/controller/HelloController.java
java
package com.example.demo.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "Hello, Spring Boot!";
}
}
解释:
· @RestController:表示这是一个返回数据的控制器。
· @GetMapping("/hello"):把 HTTP GET 请求 /hello 映射到 hello() 方法。
· 返回的字符串会直接写入 HTTP 响应体。
- 配置文件
文件:src/main/resources/application.properties
properties
spring.application.name=demo
server.port=8080
server.port=8080 表示 Tomcat 启动端口是 8080。
如果 8080 被占用,可以改成:
properties
server.port=8081
- 运行项目
方式一:IDEA 中直接运行 DemoApplication 的 main 方法。
方式二:命令行运行:
bash
mvn spring-boot:run
看到类似日志:
text
Tomcat started on port 8080
说明启动成功。
浏览器访问:
text
http://localhost:8080/hello
页面显示:
text
Hello, Spring Boot!
- 本课必须记住的 3 个注解
· @SpringBootApplication:启动类,开启自动配置和组件扫描。
· @RestController:控制器,返回 JSON 或字符串。
· @GetMapping:映射 GET 请求。
再记住一句话:
Spring Boot 约定大于配置,主类在根包,Controller 在子包,resources 下的 application.properties 会自动加载。
- 练习
练习 1:返回中文
题目要求:
把原来的 /hello 接口改成返回中文:
text
你好,Spring Boot!
答案:
java
package com.example.demo.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "你好,Spring Boot!";
}
}
解读:
@GetMapping("/hello") 仍然映射 GET 请求 /hello。
方法返回 String 时,Spring MVC 会把这个字符串直接写入 HTTP 响应体。
现代 Spring Boot 默认使用 UTF-8,所以返回中文一般不会乱码。
访问地址还是:
text
http://localhost:8080/hello
练习 2:新增当前时间接口
题目要求:
新增一个 GET 接口 /time,返回当前时间字符串。
答案:
java
package com.example.demo.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDateTime;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "你好,Spring Boot!";
}
@GetMapping("/time")
public String time() {
return LocalDateTime.now().toString();
}
}
解读:
新增方法后,加上 @GetMapping("/time"),Spring Boot 就会把 /time 请求交给这个方法。
LocalDateTime.now() 获取当前系统时间。
toString() 把时间对象转换成字符串,例如:
text
2026-09-15T10:30:45.123
访问地址:
text
http://localhost:8080/time
练习 3:接收请求参数
题目要求:
新增一个 GET 接口 /greet,接收一个 name 参数。
如果请求没有传 name,默认使用"世界"。
返回:
text
你好,xxx!
答案:
java
package com.example.demo.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDateTime;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "你好,Spring Boot!";
}
@GetMapping("/time")
public String time() {
return LocalDateTime.now().toString();
}
@GetMapping("/greet")
public String greet(@RequestParam(defaultValue = "世界") String name) {
return "你好," + name + "!";
}
}
解读:
@RequestParam 用来接收 URL 查询参数。
defaultValue = "世界" 表示如果请求里没有 name 参数,就使用"世界"。
访问不带参数:
text
http://localhost:8080/greet
返回:
text
你好,世界!
访问带参数:
text
http://localhost:8080/greet?name=张三
返回:
text
你好,张三!
注意:如果写成 ?name=,表示参数存在但值为空,这时不会使用默认值。
练习 4:修改端口
题目要求:
把项目启动端口从 8080 改成 8081。
修改后重新启动,并通过 8081 访问 /hello。
答案:
打开 src/main/resources/application.properties,改成:
properties
spring.application.name=demo
server.port=8081
然后停止项目,重新启动。
访问:
text
http://localhost:8081/hello
解读:
application.properties 是 Spring Boot 的默认配置文件,项目启动时会自动读取。
server.port 是 Spring Boot 的标准配置项,用来指定内嵌 Tomcat 的端口。
修改端口后必须重启项目才会生效。
如果 8081 也被占用,可以继续改成 8082、8083 等。
练习 5:打包运行
题目要求:
使用 Maven 把项目打包成可执行 jar,然后用 java -jar 启动。
答案:
在项目根目录执行:
bash
mvn clean package
打包成功后,执行:
bash
java -jar target/demo-0.0.1-SNAPSHOT.jar
然后访问:
text
http://localhost:8080/hello
如果你已经改过端口,就访问对应端口,例如:
text
http://localhost:8081/hello
解读:
mvn clean package 会清理并打包项目。
Spring Boot 项目引入了 spring-boot-maven-plugin 后,会生成可执行 jar。
这个 jar 里面已经包含了项目依赖和内嵌 Tomcat,所以不需要额外安装 Tomcat。
java -jar 可以直接启动这个 jar。
jar 文件名通常由 artifactId 和 version 决定。
如果 artifactId 是 demo,版本是 0.0.1-SNAPSHOT,文件名就是:
text
demo-0.0.1-SNAPSHOT.jar
- 常见错误
· 404 Not Found:多半是 Controller 不在启动类的同级包或子包中。
· 端口被占用:改 server.port=8081,或关闭占用 8080 的进程。
· 依赖下载失败:检查 Maven 配置,可以换阿里云镜像。
· Java 版本不匹配:Spring Boot 3 必须用 JDK 17+。
本课小结
你已经完成第一个 Spring Boot 项目:
· 创建 Maven 项目
· 引入 spring-boot-starter-web
· 编写 @SpringBootApplication 启动类
· 编写 @RestController
· 使用 @GetMapping 提供接口
· 通过 application.properties 修改端口
· 运行并访问 /hello
· 完成 5 个基础练习
下一课可以继续学:REST 接口、参数传递、JSON 返回、Controller / Service 分层。