我目前正在为RESTful api创建一组自定义媒体类型(例如application/vnd.mycompany.foo + xml),我试图找出两种不同的暴露超媒体链接的方式的优缺点.
如果我首先考虑其他媒体类型可能最好的起点是HTML.Html允许我创建链接,例如:
<image src="http://example.com/image.gif"/>
<a href="http://example.com/page.html"/>
<form action="http://example.com/page.html"/>
<link rel="stylesheet" type="text/css" href="theme.css" />
Run Code Online (Sandbox Code Playgroud)
这里有趣的是,在某些情况下,某些特定标签具有url属性,然后是使用rel属性定义关系的通用链接标记.
在AtomPub中,还有一些资源链接在一起的方式
<collection href="http://example.org/blog/main" >
<atom:title>My Blog Entries</atom:title>
<categories href="http://example.com/cats/forMain.cats" />
</collection>
<atom:category scheme="http://example.org/extra-cats/" term="joke" />
<atom:entry>
<link rel="edit" href="http://example.org/edit/first-post.atom"/>
</atom:entry>
Run Code Online (Sandbox Code Playgroud)
我要问的问题是,何时使用具有关系的link元素更有意义,何时将属性添加到现有元素更有意义.
例如AtomPub链接可能已经完成
<collection>
<link rel="source" href="http://example.org/blog/main"/>
<atom:title>My Blog Entries</atom:title>
<categories>
<link rel="source" href="http://example.com/cats/forMain.cats"/>
</categories>
</collection>
<atom:category term="joke">
<link rel="scheme" href="http://example.org/extra-cats/"/>
<atom:category>
<atom:entry edit="http://example.org/edit/first-post.atom"/>
Run Code Online (Sandbox Code Playgroud)
通常情况下,在写这个问题时,答案似乎显而易见.必需的链接作为属性公开,可选的链接作为元素公开.但是,我会非常有兴趣听取别人对于如何表达链接的看法.