简体   繁体   English

在RESTful API中引用URL

[英]Referring to URLs in RESTful APIs

Here are a couple of standard URLs of a RESTful API. 这是RESTful API的几个标准URL。

The first retrieves a single user, the second - a collection of users (let's say 20). 第一个检索单个用户,第二个检索一个用户集合(比方说20个)。

What is the " REST " term to refer to these URLs? 指这些URL的“ REST ”术语是什么? Is it correct to refer to them as resources? 将它们称为资源是否正确? If the first is a resource, should the second be a resource collection or should it rather be just a resource of type collection? 如果第一个是资源,第二个应该是资源集合还是应该仅仅是类型集合的资源?

Every URI on a RESTful application is a resource, this description is sufficient. RESTful应用程序上的每个URI都是资源,此描述就足够了。

Resources that link to several resources of the same type may be called collections, but there is no official name for that. 链接到相同类型的多个资源的资源可以称为集合,但是没有正式名称。 Every resource, being a collection or not, can have links. 每个资源(无论是否为集合)都可以具有链接。

Links between resources are the Hypermedia part of a RESTful system. 资源之间的链接是RESTful系统的Hypermedia部分。 Recently, a new term came up for this: HATEOAS, Hypermedia As The Engine Of Application State. 最近,出现了一个新术语:HATEOAS,超媒体作为应用程序状态的引擎。

It's a common good practice to name collections in plural, so your /users/ sample seems correct. 将集合命名为复数是一种常见的良好做法,因此您的/users/示例似乎正确。 The user 123 is a child of the users collection, so it may be better to put it under /users/123 in plural as well. 用户123是用户集合的子代,因此最好将其以复数形式放在/users/123下。

A RESTful, HATEOAS application would respond a list of links on /users/ pointing to individual resources. RESTful HATEOAS应用程序将响应/users/指向单个资源的链接列表。 Something like: 就像是:

{
    "links": [
        {
            "href" : "/users/123/"
            "title" : "Alexandre Gaigalas"
        },
        {
            "href" : "/users/125/"
            "title" : "John Doe"
        },
    ]
}

Or in XML: 或使用XML:

<link href="/users/123" title="Alexandre Gaigalas">
...

Additional information apart from the links object in JSON or tags in XML may be provided. 除了JSON中的links对象或XML中的标记之外,还可以提供其他信息。

These links stabilish a RESTful hypermedia relationship between resources. 这些链接稳定了资源之间的RESTful超媒体关系。 The samples I gave are mostly hierarchical between collections and individuals, but other types of links may be declared: 我给出的样本主要是集合和个人之间的分层结构,但可以声明其他类型的链接:

<link href="/users/123/picture.jpg" title="Alexandre Gaigalas avatar" rel="picture">

The collection terminology was created mostly for abstracting a RESTful implementation in programming languages, so developers can group and manipulate groups of similar resources more easily. 集合术语的创建主要是为了以编程语言抽象RESTful实现,因此开发人员可以更轻松地对相似资源进行分组和操作。

When present, query string parameters identify different resources as well, so /users/?since=2009 is a different from /users/ . 如果存在,查询字符串参数也会标识不同的资源,因此/users/?since=2009/users/是不同的。 They're both different resources, although very similar ones. 它们都是不同的资源,尽管非常相似。

Fragment identifiers, even though not sent to the server anyway, are considered different resources as well, so /users/123#bio is different from /users/123 . 片段标识符即使没有发送到服务器,也被认为是不同的资源,因此/users/123#bio/users/123

If possible, a more meaningful pagination is better. 如果可能,更好的分页效果更好。 Page numbers are hard to handle RESTfully because they change a lot. 页码很难更改,因为它们变化很大。 If there is a frequently updated collection (like a list of StackOverflow questions for example), the page one frequently changes and the user may loose items changing from page 1 to page 2. Most collections can be paginated by date or alphabetically. 如果有一个经常更新的集合(例如,如StackOverflow问题列表),则第一页会经常更改,并且用户可能会丢失从第1页到第2页的更改项。大多数集合可以按日期或字母分页。 Incremental page numbers aren't wrong, but there are better mechanisms. 递增的页码没有错,但是有更好的机制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM