web-services - REST Web 服务的客户端 id 生成策略

标签 web-services rest idempotent

假设我想构建一个 REST 服务来做笔记,看起来像这样:

GET    /notes/     // gives me all notes
GET    /notes/{id} // gives the note identified by {id}
DELETE /notes/{id} // delete note

PUT    /notes/{id} // creates a new note if there is no note identified by {id}
                   // otherwise the existing note is updated

因为我希望我的服务是幂等的,所以我使用 PUT 来创建和更新我的笔记,
这意味着新笔记的 id 是由客户端设置/生成的。

我想过使用 GUID/UUID,但它们很长,而且会让记住 URL 变得相当困难。同样从数据库的角度来看,当在大表中用作主键时,从性能的角度来看,这样的长字符串 id 可能会很麻烦。

你知道一个好的 id 生成策略,它可以生成短 id 并且当然可以避免冲突吗?

最佳答案

高度分布式系统(如 等)使用长 UUID/散列是有原因的,而集中式关系数据库(或 )可以简单地使用 int s。没有以分布式方式在客户端创建短 ID 的简单方法。要么服务器处理它们,要么你必须忍受浪费的 id。通常,它们包含编码时间戳、客户端/计算机 ID、散列内容等。

这就是 REST 服务通常使用的原因

POST /notes

对于非幂等保存,然后使用 Location: 的输出响应头:
Location: /notes/42

关于web-services - REST Web 服务的客户端 id 生成策略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10017338/

相关文章:

java - 如何在 Spring Boot 应用程序中引发错误情况

java - Swagger ReaderListener 未调用

vb.net - 现有连接被远程主机强行关闭

http - HTTP PUT 上下文中幂等性的含义?

firebase - 具有幂等性的云函数和 Firebase Firestore

javascript - Stripe : Is it possible to create a direct charge with idempotency

android - 从 Android 应用程序调用 Web 服务的最佳方式

java - 在 Spring MVC 中流式传输 JSON 输出

rest - 带有 Netbeans 的 Tomcat restful Web 服务数据库 (JPA) 无法工作

c# - 将 curl 命令转换为 restsharp