一、实验目的
- 掌握 SpringBoot 项目创建流程
- 理解 @RestController、@GetMapping、@PostMapping 等注解作用
- 完成 GET/POST/PUT/DELETE 四种请求的接口开发
- 使用 Postman 进行接口测试
- 将项目上传至代码仓库
二、开发环境
- JDK 1.8+
- IDEA
- SpringBoot 2.7.x
- Maven
- Postman
三、项目创建
- 使用 Spring Initializr 创建项目
- 引入 Spring Web 依赖
- 项目结构:controller、启动类、配置文件
四、核心代码开发
1. 启动类
java
运行
@SpringBootApplication
public class SpringbootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootDemoApplication.class, args);
}
}
2. Controller 层
java
运行
@RestController
@RequestMapping("/hello")
public class HelloController {
private Map<String, Integer> nameAges = new HashMap<>();
// 查询
@GetMapping
public Map<String, Integer> get() {
return nameAges;
}
// 添加
@PostMapping
public String add(@RequestParam String name,
@RequestParam Integer age) {
nameAges.put(name, age);
return "add success";
}
// 修改
@PutMapping
public String update(@RequestParam String name,
@RequestParam Integer age) {
nameAges.put(name, age);
return "update success";
}
// 删除
@DeleteMapping("/{name}")
public String delete(@PathVariable String name) {
nameAges.remove(name);
return "delete success";
}
}
五、接口测试(Postman)
- GET 查询数据
- POST 添加数据
- PUT 修改数据
- DELETE 删除数据
所有请求地址:http://localhost:8080/hello
测试结果均正常返回,功能符合预期。
六、代码仓库
项目已上传至 Gitee:https://gitee.com/you-unlucky-guy_0/introduction-to-spring.git
七、实验总结
本次实验完成了 SpringBoot 基础项目的开发,理解了 RESTful 接口风格,掌握了四种请求方式的使用,能够独立完成项目创建、编码、测试、上传仓库等流程,为后续学习 SpringBoot 打下基础。

1583

被折叠的 条评论
为什么被折叠?



