当前位置:网站首页>Rest API中PUT 与 PATCH 使用区别及幂等性分析
Rest API中PUT 与 PATCH 使用区别及幂等性分析
2022-06-09 13:44:00 【金木杂谈】
概览
本文将详细介绍HTTP PUT与PATCH的区别及使用场景,并给出示例代码。
什么时候用?
当用户需要修改对象所有数据时,应当使用PUT。
当用户只需要修改对象部分数据时,应当使用PATCH。
事实上,如果只需修改对象少数字段,却采用PUT,会显得笨拙而占用带宽,应当避免。
PUT和PATCH实例
假如我们要用REST API修改HeavyResource的多个字段。
public class HeavyResource {
private Integer id;
private String name;
private String address;
// ...
我们可以用PUT方式创建一个可以修改对象所有字段的方法
@PutMapping("/heavyresource/{id}")
public ResponseEntity<?> saveResource(@RequestBody HeavyResource heavyResource,
@PathVariable("id") String id) {
heavyResourceRepository.save(heavyResource, id);
return ResponseEntity.ok("resource saved");
}
如果只有address字段经常被客户修改,那我们就不需要传输整个heavyResource对象,而是采用PATC方法仅传输address字段数据。
我们创建HeavyResourceAddressOnly 来举例。
public class HeavyResourceAddressOnly {
private Integer id;
private String address;
// ...
}
现在我们来实现PATCH部分更新:
@PatchMapping("/heavyresource/{id}")
public ResponseEntity<?> partialUpdateName(
@RequestBody HeavyResourceAddressOnly partialUpdate, @PathVariable("id") String id) {
heavyResourceRepository.save(partialUpdate, id);
return ResponseEntity.ok("resource address updated");
}
通过PATCH,我们不需要传递整个对象数据,而仅需要传递部分需要修改的字段即可。
幂等性
HTTP方法的幂等性是指一次和多次请求某一个资源应该具有同样的副作用。

GET方法用于获取资源,不应有副作用,所以是幂等的。比如:GET http://www.bank.com/account/123,不会改变资源的状态,不论调用一次还是N次都没有副作用。
DELETE方法满足幂等性。比如:DELETE http://www.forum.com/article/123,调用一次和N次对系统产生的副作用是相同的,即删掉了id为123的帖子。
POST多次调用会产生不同结果,所以是非幂等的。比如:创建一个用户,由于网络原因或是其他原因多创建了几次,那么将会有多个用户被创建。
PUT多次调用结果一样,是幂等的。比如PUT id/123会创建一个id为123的用户,多次调用还是会创建的结果是一样的。
PATCH是非幂等的。示例如下:
a.假设请求GET /users,返回users的集合。
[{ “id”: 1, “username”: “firstuser”, “email”: “[email protected]” }]b.现在我们发送请求PATCH /users,进行更新操作(为该集合添加一个元素)
[{ “username”: “newuser”, “email”: “[email protected]” }]c.再次请求GET /users
[
{ “id”: 1, “username”: “firstuser”, “email”: “[email protected]” },
{ “id”: 2, “username”: “newuser”, “email”: “[email protected]” }
]d.重复第二步操作,现在的结果(假设用户名可重复):
[
{ “id”: 1, “username”: “firstuser”, “email”: “[email protected]” },
{ “id”: 2, “username”: “newuser”, “email”: “[email protected]” },
{ “id”: 3, “username”: “newuser”, “email”: “[email protected]” }
]
边栏推荐
- 输入一行字符(最多80个),从第location个位置开始,截取number个字符并将其输出。
- U.S. restrictions on sharing security vulnerabilities will throw stones at its own feet, and domestic systems will gain development opportunities
- Meanings of 10 important concepts and charts in Data Science
- #云原生征文#Kubernetes工作负载
- 电容电感阻抗模型分析和电源解耦电容选取经验
- MySQL transaction
- 駐美國大使館提醒在美中國公民注意暑期出行安全
- Uniswapv2 peripheral contract learning (VIII) -- exampleswaptoprice sol
- Common image segmentation methods
- 联调这夜,我把同事打了...
猜你喜欢

Understand Huawei's "Three Outlooks" in these root technologies

【深度优先搜索】玩具蛇:迷宫问题

零基础学网络:命令行(CLI)调试防火墙实战

发牌三年 5G网络深度覆盖 应用融入千行百业

Qiniu cloud backup website

Occupying the smart home market, Schneider Electric only relies on a wiser system?

Wincc中,如何利用C脚本对变量进行置位+复位+取反操作?

WordPress地址(URL)修改后打不开网站的解决方法

5年没发论文,读博想放弃?中科大博导万字自述:曾连收13封拒稿信...
![[database] final review: SQL statement, definition and judgment of normal form, ER diagram to relational mode](/img/c1/4f7777094c66216c561c71e4706ebb.png)
[database] final review: SQL statement, definition and judgment of normal form, ER diagram to relational mode
随机推荐
Little known beyond relu, it was discovered three years later: the activation functions used by Bert, gpt-2, etc
左右最值最大差
科研统计分析绘图工具GraphPad Prism 9 for Mac
临界区、事件、互斥量、 信号量--四种控制多线程同步与互斥的方法
< test> basic knowledge interview test site
【计网】思科 期末选择题复习
字节序,object类
<测试>基础知识面试考点
ACL2022 | 引入对比学习给生成的过程中加入负样本的模式使得模型能够有效地学习不同层级上的知识
美国限制分享安全漏洞将捧起石头砸自己的脚,国产系统获发展契机
多元时间序列分析 —— 因果检验
Cloud native essay kubernetes workload
[Ji Wang] review of Cisco final multiple choice questions
UniswapV2周边合约学习(八)-- ExampleSwapToPrice.sol
Understand Huawei's "Three Outlooks" in these root technologies
为什么 SQL 语句使用了索引,但却还是慢查询?
智慧农业小麦室外病虫害防治规范,北斗农业
发牌三年 5G网络深度覆盖 应用融入千行百业
How can minority majors solve the problem of "growth"?
qt连接本地mysql时连不上