全局资源 URI(即可寻址性)有什么好处?

发布于 2024-07-05 23:37:31 字数 565 浏览 4 评论 0原文

与使用专有 id 格式相比,使用全局唯一 URI(如 REST 所做的)引用资源有什么好处?

例如:

  1. http://host.com/student/5
  2. http://host.com/student?id=5

在第一种方法中,整个 URL 就是 ID。 在第二种方法中,只有 5 是 ID。 第一种方法相对于第二种方法有什么实际好处?

为什么 REST(似乎)不遗余力地提倡第一种方法?

-- 编辑:

我的问题很混乱,因为它确实提出了两个不同的问题:

  1. 可寻址性的好处是什么?
  2. 上面看到的两种 URI 形式有什么区别。

我已经用我自己的帖子回答了下面的两个问题。

What is the benefit of referencing resources using globally-unique URIs (as REST does) versus using a proprietary id format?

For example:

  1. http://host.com/student/5
  2. http://host.com/student?id=5

In the first approach the entire URL is the ID. In the second approach only the 5 is the ID. What is the practical benefit of the first approach over the second?

Why does REST (seem to) go out of its way to advocate the first approach?

-- EDIT:

My question was confusing because it really asked two separate questions:

  1. What is the benefit of addressibility?
  2. What is the difference between the two URI forms seen above.

I've answered both questions below using my own post.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

指尖上得阳光 2024-07-12 23:37:38

我认为这取决于你想要遵守风水原则的程度。

I think it comes down to how closely you want to adhere to the principles of feng shui.

黎歌 2024-07-12 23:37:38

从 REST 角度来看,这两个 URI 都是有效的,但只需意识到 Web 缓存对查询字符串参数的处理方式非常不同。
如果您想利用缓存来发挥自己的优势,那么我建议您不要使用查询字符串参数来标识您的资源。

Both URIs are valid from a REST perspective, however just realize that web caches treat the querystring parameters very differently.
If you want to use caching to your advantage then I suggest that you do not use a query string parameter to identify your resource.

夢归不見 2024-07-12 23:37:37

正如 Ólafur 提到的,前一个 url 的清晰度是一个好处。

另一个是实施灵活性。

假设学生 5 很少变化。 如果您使用 REST 样式的 url,您可以选择提供静态文件而不是运行代码。 在 Rails 中,对 Students/5 的第一个请求通常会在您的 Web 根目录下创建一个缓存的 html 文件。 该文件用于服务后续请求,无需接触后端。 当然,这种方法没有任何特定于 Rails 的内容。

后面的 url 不允许这样做。 静态页面的名称中不能包含 url 变量 (?, =)。

As Ólafur mentioned, The clarity of the former url is one benefit.

Another is implementation flexibility.

Let's say that student 5 changes infrequently. If you use the REST-style url you have the option of serving a static file instead of running code. In Rails it is common that the first request to students/5 would create a cached html file under your web root. That file is used to serve subsequent requests w/o touching the backend. Naturally, there's nothing rails specific about that approach.

The later url wouldn't allow for this. You can't have url variables (?, =) in the names of static pages.

不回头走下去 2024-07-12 23:37:36

第一个更美观。

从技术上讲,没有什么区别,但如果可以的话,请使用前者。

The first is more aesthetically pleasing.

Technically there is no difference, but use the former when you can.

森林迷了鹿 2024-07-12 23:37:35

主要是搜索引擎优化。

在我看来,这也让它们更容易记住、更干净、更专业。

Search engine optimization mostly.

It also makes them easier to remember, and cleaner, more professional looking in my opinion.

ぃ双果 2024-07-12 23:37:35

我将回答我自己的问题:

1)为什么 URI 很重要?

我将引用 Leonard Richardson 和 Sam Ruby 所著的《RESTful Web Services》(ISBN:978-0-596-52926-0):

考虑一个真实的 URI,它在类型“资源目录”中命名资源
jellyfish”:http://www.google.com/search?q=jellyfish 。 水母搜索就像
更像是一个真实的 URI,例如 http://www.google.com。 如果 HTTP 不可寻址,或者 Google
搜索引擎不是可寻址的网络应用程序,我无法发布它
书中的 URI。 我必须告诉你:“打开 google.com 的网络连接,输入“jellyfish”
在搜索框中,然后点击“Google 搜索”按钮。

这不是学术上的担忧。 直到 20 世纪 90 年代中期,ftp:// URI
由于描述 FTP 站点上的文件而变得流行,人们必须编写
例如:“在 ftp.example.com 上启动匿名 FTP 会话。 然后
切换到目录 pub/files/ 并下载文件 file.txt。” 制作的 URI
FTP 与 HTTP 一样可寻址。 现在人们只是写:“下载 ftp://
ftp.example.com/pub/files/file.txt”。 步骤是相同的​​,但现在它们
可以通过机器进行。

[...]

可寻址性是 Web 应用程序的最佳优点之一。 它使您可以轻松地
客户以原始设计者从未想象过的方式使用网站。

2)可寻址性有什么好处?

遵循服务器提供的 URI 比自己构建它们要容易得多。 当资源关系变得过于复杂而无法用简单规则表达时尤其如此。 在服务器中编写一次逻辑比在众多客户端中重新实现它更容易。

即使各个资源 URI 保持不变,资源之间的关系也可能会发生变化。 例如,如果 Google 地图要更改其地图图块的比例,计算相对图块位置的客户端就会崩溃。

3) URI 相对于自定义 ID 有什么好处?

自定义 ID 唯一标识资源。 URI 更进一步,告诉您在哪里可以找到它。 这简化了客户端逻辑。

I will answer my own question:

1) Why are URIs important?

I'll quote from RESTful Web Services by Leonard Richardson and Sam Ruby (ISBN: 978-0-596-52926-0):

Consider a real URI that names a resource in the genre “directory of resources about
jellyfish”: http://www.google.com/search?q=jellyfish. That jellyfish search is just as
much a real URI as http://www.google.com. If HTTP wasn’t addressable, or if the Google
search engine wasn’t an addressable web application, I wouldn’t be able to publish that
URI in a book. I’d have to tell you: “Open a web connection to google.com, type ‘jellyfish’
in the search box, and click the ‘Google Search’ button.

This isn’t an academic worry. Until the mid-1990s, when ftp:// URIs
became popular for describing files on FTP sites, people had to write
things like: “Start an anonymous FTP session on ftp.example.com. Then
change to directory pub/files/ and download file file.txt.” URIs made
FTP as addressable as HTTP. Now people just write: “Download ftp://
ftp.example.com/pub/files/file.txt.” The steps are the same, but now they
can be carried out by machine.

[...]

Addressability is one of the best things about web applications. It makes it easy for
clients to use web sites in ways the original designers never imagined.

2) What is the benefit of addressibility?

It is far easier to follow server-provided URIs than construct them yourself. This is especially true as resource relationships become too complex to be expressed in simple rules. It's easier to code the logic once in the server than re-implement it in numerous clients.

The relationship between resources may change even though the individual resource URIs remain unchanged. For example, if Google Maps were to change the scale of their map tiles, clients that calculate relative tile positions would break.

3) What is the benefit of URIs over custom IDs?

Custom IDs identify a resource uniquely. URIs go a step further by telling you where to find it. This simplifies the client logic.

故事灯 2024-07-12 23:37:34

当我看到这样的 uri 时,最重要的是普通用户能够记住该 uri。

我们这些极客可以接受问号并获取变量,但如果有人记得 http://www.host.com /users/john 而不是 http://www.host.com /?view=users&name=john,那么这是一个巨大的好处。

The main thing when i see uri's like that is a normal user would be able to remember that uri.

Us geeks are fine with question marks and get variables, but if someone remembers http://www.host.com/users/john instead of http://www.host.com/?view=users&name=john, then that is a huge benefit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文