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";
}
相关推荐
一只猿Hou8 分钟前
java分页插件| MyBatis-Plus分页 vs PageHelper分页:全面对比与最佳实践
java·mybatis
程序员弘羽14 分钟前
C++ 第四阶段 内存管理 - 第二节:避免内存泄漏的技巧
java·jvm·c++
旷世奇才李先生18 分钟前
Tomcat 安装使用教程
java·tomcat
勤奋的知更鸟31 分钟前
Java 编程之策略模式详解
java·设计模式·策略模式
qq_49244844633 分钟前
Java 访问HTTP,信任所有证书,解决SSL报错问题
java·http·ssl
大只鹅34 分钟前
Springboot3.3.4使用spring-data-elasticsearch整合Elasticsearch7.12.1
spring boot·elasticsearch
爱上语文36 分钟前
Redis基础(4):Set类型和SortedSet类型
java·数据库·redis·后端
lifallen1 小时前
Paimon vs. HBase:全链路开销对比
java·大数据·数据结构·数据库·算法·flink·hbase
1.01^10001 小时前
[6-02-01].第05节:配置文件 - YAML配置文件语法
spring boot
笑衬人心。1 小时前
初学Spring AI 笔记
人工智能·笔记·spring