本文介绍了如何移动 REST 资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将资源从 /buckets/1 移动到 /buckets/2 使得:

I'm trying to move a resource from /buckets/1 to /buckets/2 such that:

  • /buckets/1 = foo
  • /buckets/2 = HTTP 404
  • /buckets/1 = HTTP 301 到/buckets/2
  • /buckets/2 = foo

要求服务器以这种方式移动资源的 RESTful 方式是什么?

What's a RESTful way of asking the server to move a resource in this manner?

推荐答案

回答我自己的问题:

  • 为了便于讨论,我们假设我们将球"存储在桶中
  • 首先要注意的是,球的生命周期并不由其包含的桶决定(将球从一个桶移动到另一个桶不会删除旧球).因此,我们应该将球提升为顶级资源:/balls
  • REST 似乎在符号链接方面效果最好,而不是内联值,所以不是 GET/buckets/1 返回桶中球的值,让我们让它返回的 URI取而代之的是球.
  • For the sake of this discussion let's assume that we are storing "balls" in buckets
  • The first thing to notice is that a ball's life-cycle is not determined by its containing bucket (moving a ball from one bucket to another does not delete the old ball). As such we should promote balls to a top-level resource: /balls
  • REST seems to work best in terms of symbolic links as opposed to inline values, so instead of GET /buckets/1 returning the value of the ball in the bucket let's have it return the URI of the ball instead.

然后我们可以按如下方式移动球:

We can then move balls as follows:

(examine original state)
GET /buckets/1: "balls = {'/balls/1'}"
GET /buckets/2: "balls = {}"
GET /balls/1: "bucket = /buckets/1"

(move ball into bucket #2)
PUT /balls/1: "bucket = /buckets/2"

(examine new state)
GET /buckets/1: "balls = {}"
GET /buckets/2: "balls = {'/balls/1'}"
GET /balls/1: "bucket = /buckets/2"

最终结果:当球在桶中移动时,球的身份保持一致,并且(最重要的是)这个操作是原子的.

End-result: the ball's identity remains consistent as it moves across buckets and (most importantly) this operation is atomic.

这篇关于如何移动 REST 资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 22:27