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";
}
相关推荐
gadiaola几秒前
【SSM面试篇】Spring、SpringMVC、SpringBoot、Mybatis高频八股汇总
java·spring boot·spring·面试·mybatis
写不出来就跑路2 分钟前
WebClient与HTTPInterface远程调用对比
java·开发语言·后端·spring·springboot
Cyanto8 分钟前
深入MyBatis:CRUD操作与高级查询实战
java·数据库·mybatis
麦兜*1 小时前
Spring Boot 集成Reactive Web 性能优化全栈技术方案,包含底层原理、压测方法论、参数调优
java·前端·spring boot·spring·spring cloud·性能优化·maven
天上掉下来个程小白1 小时前
MybatisPlus-06.核心功能-自定义SQL
java·spring boot·后端·sql·微服务·mybatisplus
知了一笑1 小时前
独立开发第二周:构建、执行、规划
java·前端·后端
寻月隐君1 小时前
想用 Rust 开发游戏?这份超详细的入门教程请收好!
后端·rust·github
晴空月明1 小时前
分布式系统高可用性设计 - 缓存策略与数据同步机制
后端
今天背单词了吗9802 小时前
算法学习笔记:17.蒙特卡洛算法 ——从原理到实战,涵盖 LeetCode 与考研 408 例题
java·笔记·考研·算法·蒙特卡洛算法
Dcs2 小时前
从 C 到 Rust:一位开发者的 `tmux` 全面移植之旅
java