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

一、RESTful 风格

1、基本介绍
  1. RESTful 风格基于 HTTP 协议,强调资源的表示,即一目了然,例如,普通风格的请求 /reqUser?id=【id】 在 RESTful 风格下就是 /reqUser/{【id】}

  2. RESTful 风格与普通风格的差异主要体现在理念上,而编码风格差异不大,所以我们不需要有什么心理负担

2、RESTful 风格四大请求
(1)GET 请求
  1. 通常用于请求资源

  2. 通常在路径中带参

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

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

(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. 使用 @PathVariable 来映射请求参数


二、RESTful 风格基本使用

0、控制器
  1. 在 Spring Boot 中,使用 @RestController 注解定义控制器

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

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

    return staff;
}
(2)带参
  • 根据请求参数 id,从 Map 中返回对应的 Staff 对象
java 复制代码
@GetMapping("/testRestfulGetCarryData")
public Staff testGetRestfulCarryData(@PathVariable 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("/testRestfulPost")
public Staff testRestfulPost(@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("/testRestfulPut")
public String testRestfulPut(@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("/testRestfulDelete")
public String testRestfulDelete(@PathVariable 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";
}
相关推荐
极客先躯10 分钟前
高级java每日一道面试题-2026年04月04日-实战篇[Docker]-如何排查容器存储挂载失败的问题?
java·运维·docker·容器·高级面试
Hesionberger14 分钟前
动态规划与二分法破解最长递增子序列
java·数据结构·python·算法·leetcode
CodeStats26 分钟前
【Linux IO】从文件描述符到硬件中断:Linux IO 系统底层完全拆解
java·linux·开发语言·io·socket
大阿明31 分钟前
C++智能指针与RAII机制精讲:彻底根治内存泄漏与野指针
java·jvm·c++
一生了无挂34 分钟前
C++面向对象核心精讲:类、对象、封装、权限与生命周期全解析
java·jvm·c++
aaPIXa62234 分钟前
C++ 用 Claude Code 的 defending-code-reference-harness:C/C++ 内存漏洞自动发现与修复 Pipeline
java·c语言·c++
她说..37 分钟前
Apache Commons Lang3 Pair 完整版实战详解
java·spring·java-ee·apache·springboot
砍材农夫44 分钟前
redis|spring-boot redis geo|附近定位功能
java·spring boot·redis
满怀冰雪1 小时前
第29篇-状态压缩DP-当状态很多时如何降维优化
java·算法·动态规划
weixin_446729161 小时前
javaweb--文件上传
java