自链接在超媒体 API 中的重要性是什么? [英] What is the importance of the self link in hypermedia APIs?

查看:60
本文介绍了自链接在超媒体 API 中的重要性是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 REST 上阅读的所有文章和书籍都重复了在超媒体响应中添加自我"rel 链接的重要性,但它们都对原因和用例进行了阐述.

All the articles and books I read on REST repeat the importance of adding "self" rel links to your hypermedia responses but they're all light on the reasons and use cases.

为什么要添加自链接,它有什么用处?

Why should you add a self link and how is it useful?

推荐答案

主要原因是客户端(甚至一些服务器)不将表示的位置与表示一起存储.例如,如果您 wget http://.../foo.json,则表示将保存到磁盘,但获取它的 URI 不会.如果表示中没有嵌入self"链接,这会导致两个问题:

The main reason is that clients (and even some servers) do not store the location of a representation with the representation. For example, if you wget http://.../foo.json, the representation will be saved to disk, but the URI at which it was fetched will not be. If there is no "self" link embedded in the representation, this causes two problems:

  1. 文档中的相对链接可能不再有解析的基础,因此将被破坏";和

  1. Relative links in the document may no longer have a base against which to resolve, and will therefore be "broken"; and

客户将没有嵌入的概念,例如去哪里.PUT 如果他们修改了文档,则将其返回给服务器.少数客户独立维护这些信息,但大多数客户没有.

The client will have no embedded concept of where to e.g. PUT the document back to a server if they modify it. A few clients maintain this information independently, but most do not.

重要的是要了解表示可能在 HTTP 对话之外有生命力,甚至可能通过其他协议(电子邮件、FTP、书籍等)传输.因此,经验丰富的媒体类型设计师通常会包含一个自我"链接.

It's important to understand that representations may have a life well outside the HTTP conversation, and may even be transferred via other protocols (e-mail, FTP, in a book, etc). An experienced media-type designer will therefore typically include a "self" link.

这篇关于自链接在超媒体 API 中的重要性是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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