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";
}
相关推荐
一只叫煤球的猫5 小时前
写代码很6,面试秒变菜鸟?不卖课,面试官视角走心探讨
前端·后端·面试
bobz9655 小时前
tcp/ip 中的多路复用
后端
bobz9656 小时前
tls ingress 简单记录
后端
皮皮林5517 小时前
IDEA 源码阅读利器,你居然还不会?
java·intellij idea
你的人类朋友7 小时前
什么是OpenSSL
后端·安全·程序员
bobz9657 小时前
mcp 直接操作浏览器
后端
前端小张同学9 小时前
服务器部署 gitlab 占用空间太大怎么办,优化思路。
后端
databook10 小时前
Manim实现闪光轨迹特效
后端·python·动效
武子康10 小时前
大数据-98 Spark 从 DStream 到 Structured Streaming:Spark 实时计算的演进
大数据·后端·spark
该用户已不存在10 小时前
6个值得收藏的.NET ORM 框架
前端·后端·.net