REST-统一接口到底是什么意思? [英] REST - What exactly is meant by Uniform Interface?

查看:523
本文介绍了REST-统一接口到底是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

维基百科具有:

统一界面

统一接口约束是任何REST服务设计的基础.[14]统一的界面简化并解耦了体系结构,从而使每个部分都可以独立发展.该界面的四个指导原则是:

The uniform interface constraint is fundamental to the design of any REST service.[14] The uniform interface simplifies and decouples the architecture, which enables each part to evolve independently. The four guiding principles of this interface are:

资源标识

在请求中标识单个资源,例如在基于Web的REST系统中使用URI.资源本身在概念上与返回给客户端的表示形式是分开的.例如,服务器可以从数据库中以HTML,XML或JSON格式发送数据,这些数据都不是服务器的内部表示形式,并且无论哪种资源都是相同的.

Individual resources are identified in requests, for example using URIs in web-based REST systems. The resources themselves are conceptually separate from the representations that are returned to the client. For example, the server may send data from its database as HTML, XML or JSON, none of which are the server's internal representation, and it is the same one resource regardless.

通过这些表示方式操纵资源

当客户端持有资源的表示形式(包括附加的任何元数据)时,它具有足够的信息来修改或删除资源.

When a client holds a representation of a resource, including any metadata attached, it has enough information to modify or delete the resource.

自我描述性消息

每条消息都包含足够的信息来描述如何处理该消息.例如,可以由Internet媒体类型(以前称为MIME类型)指定要调用的解析器.响应还明确表明其可缓存性.

Each message includes enough information to describe how to process the message. For example, which parser to invoke may be specified by an Internet media type (previously known as a MIME type). Responses also explicitly indicate their cacheability.

使用超媒体作为应用程序状态(A.K.A. HATEOAS)的引擎

客户端仅通过服务器在超媒体中动态标识的动作(例如,通过超文本中的超链接)进行状态转换.除了应用程序的简单固定入口点外,客户端不假定任何特定动作可用于除先前从服务器接收到的表示中描述的资源以外的任何特定资源.

Clients make state transitions only through actions that are dynamically identified within hypermedia by the server (e.g., by hyperlinks within hypertext). Except for simple fixed entry points to the application, a client does not assume that any particular action is available for any particular resources beyond those described in representations previously received from the server.

我正在听有关该主题的演讲,演讲者说:

I'm listening to a lecture on the subject and the lecturer has said:

"当有人使用我们的API时,如果您能够获取客户对象并且知道有订单对象,那么您应该能够以与获取客户对象相同的模式获取订单对象.这些URI看起来会彼此相似."

"When someone comes up to our API, if you are able to get a customer object and you know there are order objects, you should be able to get the order objects with the same pattern that you got the customer objects from. These URI's are going to look like each other."

这让我觉得很不对.与其说URI看起来是什么,不如说它具有一致性,因为这是URI的使用方式(标识资源,通过表示,自我描述性消息和仇恨情绪.

This strikes me as wrong. It's not so much about what the URI's look like or that there is consistency as it is the way in which the URI's are used (identify resources, manipulate the resources through representations, self-descriptive messages, and hateoas).

我完全不认为这是统一接口的意思.到底是什么意思?

I don't think that's what Uniform Interface means at all. What exactly does it mean?

推荐答案

使用接口将类与依赖关系的实现分离开来是一个很老的概念.令您惊讶的是,我感到很惊讶...

Using interfaces to decouple classes from the implementation of their dependencies is a pretty old concept. I am surprised that you haven't heard of it...

通过REST,您使用相同的概念来使客户端与REST服务的实现脱钩.为了定义这样的接口(客户端和服务之间的契约),您必须使用标准.这是因为如果您想要一个由REST服务组成的Internet规模的网络,则必须实施全局性概念,例如使它们相互理解的标准.

By REST you use the same concept to decouple the client from the implementation of the REST service. In order to do define such an interface (contract between the client and the service) you have to use standards. This is because if you want an internet size network of REST services, you have to enforce global concepts, like standards to make them understand each other.

  • 资源标识-您使用URI(IRI)标准来标识资源.在这种情况下,资源就是网络文档.

  • Identification of resources - You use the URI (IRI) standard to identify a resource. In this case a resource is a web document.

通过这些表示来操纵资源-您使用HTTP标准来描述通信.因此,例如GET意味着您要检索有关URI标识的资源的数据.您可以使用HTTP方法和URI描述操作.

Manipulation of resources through these representations - You use the HTTP standard to describe communication. So for example GET means that you want to retrieve data about the URI identified resource. You can describe an operation with a HTTP method and an URI.

自描述消息-您使用标准MIME类型和(标准)RDF vocabs使消息具有自描述性.因此,客户端可以通过检查语义来查找数据,而不必知道服务使用的特定于应用程序的数据结构.

Self-descriptive messages - You use standard MIME types and (standard) RDF vocabs to make messages self-descriptive. So the client can find the data by checking the semantics, and it don't have to know the application specific data structure the service uses.

超媒体作为应用程序状态(即HATEOAS)的引擎-您使用超链接以及可能的URI模板将客户端与应用程序特定的URI结构分离.您可以使用语义来注释这些超链接,例如IANA链接关系,因此客户将理解它们的含义.

Hypermedia as the engine of application state (A.K.A. HATEOAS) - You use hyperlinks and possibly URI templates to decouple the client from the application specific URI structure. You can annotate these hyperlinks with semantics e.g. IANA link relations, so the client will understand what they mean.

这篇关于REST-统一接口到底是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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