Spring Boot 四大请求编码模板(GET、POST、PUT、DELETE)

一、四大请求引入

1、GET 请求
  1. 通常用于请求资源

  2. 通常在路径中带参

  3. 在 Spring Boot 中,使用 @GetMapping@RequestMapping(method = RequestMethod.GET) 注解来映射请求方式与请求路径

  4. 使用 @RequestParam 来映射请求参数

2、POST 请求
  1. 通常用于提交资源

  2. 通常在请求体中带参

  3. 在 Spring Boot 中,使用 @PostMapping@RequestMapping(method = RequestMethod.POST) 注解来映射请求方式与请求路径

  4. 使用 @RequestBody 来映射请求体

3、PUT 请求
  1. 通常用于修改资源

  2. 通常在路径中带参

  3. 在 Spring Boot 中,使用 @PutMapping@RequestMapping(method = RequestMethod.GET) 注解来映射请求方式与请求路径

  4. 使用 @RequestBody 来映射请求体

4、DELETE 请求
  1. 通常用于删除资源

  2. 通常在请求体中带参

  3. 在 Spring Boot 中,使用 @DeleteMapping@RequestMapping(method = RequestMethod.DELETE) 注解来映射请求方式与请求路径

  4. 使用 @RequestParam 来映射请求参数


二、四大请求基本使用

0、控制器
  1. 在 Spring Boot 中,使用 @Controller 注解定义控制器,使用 @ResponseBody 注解定域返回值作为响应主体内容

  2. 但这两个注解可以同时用一个注解 @RestController 代替,这里我推荐写少不写多

  3. 使用 @RequestMapping 注解来映射请求基础路径

java 复制代码
@Controller
@ResponseBody
@RequestMapping("/myTest")
public class MyTestController {}
java 复制代码
// 推荐这种写法

@RestController
@RequestMapping("/myTest")
public class MyTestController {}
1、GET 请求
(1)不带参
  • 返回一个Staff 对象实例
java 复制代码
@GetMapping("/testGet")
public Staff testGet() {
    Staff staff = new Staff(1, "jack", 10);

    return staff;
}
(2)带参
  • 根据请求参数 id,从 Map 中返回对应的 Staff 对象
java 复制代码
@GetMapping("/testGetCarryData")
public Staff testGetCarryData(@RequestParam("id") Integer id) {
    HashMap<Integer, Staff> staffMap = new HashMap<>();
    staffMap.put(1, new Staff(1, "jack", 10));
    staffMap.put(2, new Staff(2, "tom", 20));
    staffMap.put(3, new Staff(3, "smith", 30));

    return staffMap.get(id);
}
2、POST 请求
  • 根据请求体 TestPostDTO 中的 id,从 Map 中返回对应的 Staff 对象
java 复制代码
@PostMapping("/testPost")
public Staff testPost(@RequestBody TestPostDTO testPostDTO) {
    HashMap<Integer, Staff> staffMap = new HashMap<>();
    staffMap.put(1, new Staff(1, "jack", 10));
    staffMap.put(2, new Staff(2, "tom", 20));
    staffMap.put(3, new Staff(3, "smith", 30));

    return staffMap.get(testPostDTO.getId());
}
3、PUT 请求
  • 根据请求体 TestPutDTO 中的 id,找到对应的 Staff 对象,更新其年龄
java 复制代码
@PutMapping("/testPut")
public String testPut(@RequestBody TestPutDTO testPutDTO) {
    HashMap<Integer, Staff> staffMap = new HashMap<>();
    staffMap.put(1, new Staff(1, "jack", 10));
    staffMap.put(2, new Staff(2, "tom", 20));
    staffMap.put(3, new Staff(3, "smith", 30));

    Staff staff = staffMap.get(testPutDTO.getId());
    staff.setAge(testPutDTO.getAge());

    return "testPut OK";
}
4、DELETE 请求
  • 根据请求参数 id,从 Map 中返回删除的 Staff 对象
java 复制代码
@DeleteMapping("/testDelete")
public String testDelete(@RequestParam("id") Integer id) {
    HashMap<Integer, Staff> staffMap = new HashMap<>();
    staffMap.put(1, new Staff(1, "jack", 10));
    staffMap.put(2, new Staff(2, "tom", 20));
    staffMap.put(3, new Staff(3, "smith", 30));

    staffMap.remove(id);

    return "testDelete OK";
}
相关推荐
Chase_Mos4 分钟前
Spring 必会之微服务篇(1)
java·spring·微服务
懵逼的小黑子1 小时前
Django 项目的 models 目录中,__init__.py 文件的作用
后端·python·django
小林学习编程2 小时前
SpringBoot校园失物招领信息平台
java·spring boot·后端
撸码到无法自拔2 小时前
docker常见命令
java·spring cloud·docker·容器·eureka
愿你天黑有灯下雨有伞2 小时前
Spring Boot整合Kafka实战指南:从环境搭建到消息处理全解析
spring boot·kafka·linq
heart000_13 小时前
IDEA 插件推荐:提升编程效率
java·ide·intellij-idea
Clf丶忆笙3 小时前
SpringBoot异步处理@Async深度解析:从基础到高阶实战
spring boot
ŧ榕树先生3 小时前
查看jdk是否安装并且配置成功?(Android studio安装前的准备)
java·jdk
未来的JAVA高级开发工程师3 小时前
适配器模式
java
LUCIAZZZ4 小时前
JVM之内存管理(一)
java·jvm·spring·操作系统·springboot