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";
}
相关推荐
C++chaofan2 分钟前
项目中基于redis实现缓存
java·数据库·spring boot·redis·spring·缓存
百***86465 分钟前
springboot整合libreoffice(两种方式,使用本地和远程的libreoffice);docker中同时部署应用和libreoffice
spring boot·后端·docker
MZ_ZXD00112 分钟前
springboot流浪动物救助平台-计算机毕业设计源码08780
java·spring boot·后端·python·spring·flask·课程设计
没有bug.的程序员14 分钟前
Spring 全家桶在大型项目的最佳实践总结
java·开发语言·spring boot·分布式·后端·spring
掘金码甲哥15 分钟前
🎨 新来的外包,在大群分享了它的限流算法的实现
后端
在坚持一下我可没意见16 分钟前
Spring IoC 入门详解:Bean 注册、注解使用与 @ComponentScan 配置
java·开发语言·后端·spring·rpc·java-ee
用户214118326360217 分钟前
Claude Skills实战指南:Skill Seekers 自动生成 SiliconFlow API 技能
后端
b***91036 分钟前
【SpringBoot3】Spring Boot 3.0 集成 Mybatis Plus
android·前端·后端·mybatis
leonardee1 小时前
Android和JAVA面试题相关资料
java·后端
w***4241 小时前
Spring Boot 条件注解:@ConditionalOnProperty 完全解析
java·spring boot·后端