REST API和UUID [英] Rest API and UUID

查看:153
本文介绍了REST API和UUID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

原因之一,恐怕也是主要原因之一,使用UUID是避免了集中式点负责创建和资源分配的ID。

One of the reasons, and probably the main one, to use UUID is to avoid having a "centralized" point responsible for creating and assigning ids to resources.

这意味着,对于REST API的,客户端可以(也应该)能够生成,并给予的UUID某个资源时,POST首次该特定资源。这将最小化与成功张贴首次一个资源,但没有得到该ID背面作为响应(例如连接问题)相关的问题。这可能会导致一些客户的一个新的职位,产生重复的资源。

That means that, for REST APIs, the clients could (and should) be able to generate, and give the UUID for a certain resource when they POST that specific resource for the first time. That would minimize problems related with successfully posting a resource for the first time but not getting the ID back as response (connectivity problems for example). That can result in a new post for some of the clients, generating duplicated resources.

我的问题是:


  • 将客户端生成和REST API的暴露有UUID是最好的做法?

  • 还有没有其他的替代品是什么?

推荐答案

REST并不真正关心,如果是由服务器或客户端所产生的UUID。它只需要在URI形式的独特的资源标识符。
URI中有什么样的形式,是不是客户端和服务器的重要 - 只有它们是唯一的,并且可以通过客户端(HATEOAS)获得。你需要当然也是在服务器端,它能够创建子资源,为您和了解您想要提供的UUID而不是生成自己一个人的资源。取而代之的是UUID的,你可以F.E.还使用博客,帖子的网址恩codeD标题或类似这样的问题,哈希值和问题标题的组合 31584303 / REST的API和-UUID 来唯一地标识资源。

REST does not really care if the UUID is generated by the server or by the client. It just needs a unique resource-identifier in form of an URI. What form the URI has, is not important to clients and servers - only that they are unique and may be obtained by clients (HATEOAS). You need of course also a resource on the server side which is able to create the sub-resource for you and understands that you want to provide the UUID instead of generating an own one. Instead of a UUID you could f.e. also use a url-encoded title of a blog-post or like this question a combination of hash-value and question-title 31584303/rest-api-and-uuid to uniquely identify a resource.

由客户端生成UUID是不能使用的奥芬在实践中我的意见,但我可能是错在这个问题上。实际的问题是相反,为什么要客户真正提供一个自己的UUID,而不是让服务器创建一个吗?客户端是,国际海事组织,仅在获取数据给服务器,并具有某种方式在以后的某个点,这将通过在POST请求的响应中返回的位置标头被提供到检索它感兴趣。

Generating a UUID by a client is in my opinion not used that ofen in practice, but I may be wrong on this matter. The actual question is rather, why should a client really provide an own UUID instead of letting the server create one? The client is, IMO, only interested in getting the data to the server and having some way to retrieve it at some later point, which will be provided through the location header returned in the response of a POST request.

如果连接问题是一个实际的关注,可以让客户端发送一个空POST创建资源和头内发送的位置。内容是不是通过PUT请求加入。

If connection issues are an actual concern, you could let the client send an empty POST to create a resource and send back the location within the header. The content is than added via a PUT request.

有仍然可以介入了一些连接问题:

There still can be some connection issues involved:


  • 请求不会到达服务器

  • 响应不到达客户端

虽然引物之一就是为客户端没有问题以及对服务器不执行操作,所述请求可以简单地重新发送,后者实际上将创建在服务器侧的资源,虽然链接永远不会到达客户端。因此,实际的资源处于unreferencable状态,除非你提供一个方法来遍历所有资源,其中还包含空的。

While the primer one is no issue for the client as well as for the server as no operation is executed and the request can simply be resent, the latter one will actually create a resource at the server side, though the link never reaches the client. Therefore the actual resource is in an unreferencable state unless you provide a way to iterate over all resources, which contains also the empty ones.

一个服务器可以具有其中给定的时间量之后删除空资源背面的清理线程。如果客户端发送一个空的进一步POST请求,但此时也接收创建的资源的URI,他可以通过 PUT 更新资源的状态。 PUT是​​幂等。如果该服务器未接收到该请求时,客户端可以简单地重新发送它。 PUT有更新现有或创建新的资源,如果它是尚未公布的语义。因此,服务器可以创建在与所提供的内容的情况下,该资源。如果请求确实到达服务器,进一步的更新不会改变资源的状态。

A server can have a cleanup thread in the back which removes empty resources after a given amount of time. If a client sends a further empty POST request but this time also receives the URI of the created resource, he can update the state of the resource via PUT. PUT is idempotent. If the server did not receive the request, the client can simply resent it. PUT has the semantic of updating existing or creating a new resource if it is not yet available. So, the server can create the resource in that case with the provided content. If the request did reach the server, a further update does not change the state of the resource.

这篇关于REST API和UUID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆