简体   繁体   English

REST中的资源和资源表示有什么区别?

[英]What is the difference between resource and resource representation in REST?

I am new to REST and just started reading some tutorials. 我是REST新手,刚开始阅读一些教程。

One thing that really confuses me is: what comes in txt/xml/json form: the resources or the resource representations? 让我感到困惑的一件事是: txt / xml / json形式的内容:资源还是资源表示? Must be the latter, right? 必须是后者,对吗? Since resources can be a video, audio or other MIME types. 由于资源可以是视频,音频或其他MIME类型。

Take the example below. 以下面的例子为例。 Let's say I am given a description like 'a RESTful service where User is a resource which is represented using the following XML format': 假设我给出的描述类似于“RESTful服务,其中User是使用以下XML格式表示的资源”:

<user>
   <id>1</id>
   <name>Mahesh</name>
   <profession>Teacher</profession>
</user>

or JSON format: 或JSON格式:

{
   "id":1,
   "name":"Mahesh",
   "profession":"Teacher"
}

Then, when I use HTTP GET to access the resource, what data do I actually get back? 然后,当我使用HTTP GET访问资源时, 我实际上会得到什么数据? Do I get '1, Mahesh, Teacher' since this is the real data excluding the format, or do I get the whole xml or json 'object' that contains both the data and the data representation? 我得到'1,Mahesh,老师',因为这是排除格式的真实数据,还是我得到包含数据和数据表示的整个xml或json'对象'?

What if User has an image property? 如果用户有图像属性怎么办? What kind of 'package' and in which form will HTTP deliver it to me: the image itself or a link to the image? HTTP将什么样的“包”以及以何种形式提供给我:图像本身或图像的链接?

EDIT 编辑

Another example here: 另一个例子:

http://maps.googleapis.com/maps/api/geocode/xml?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false http://maps.googleapis.com/maps/api/geocode/xml?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false

Here should I understand that the returned resource itself is an XML file, or the resource is NOT an XML file, but some data that's embedded in XML resource representation is? 我应该理解返回的资源本身是XML文件,或者资源不是XML文件,但是嵌入在XML资源表示中的一些数据是?

And what if the resource I want contains images, videos, etc.? 如果我想要的资源包含图像,视频等怎么办? Those are not text data that can be embedded in XML or JSON format--in that case, what do I get? 那些不是可以嵌入XML或JSON格式的文本数据 - 在这种情况下,我得到了什么?

Resource 资源

The concept of a REST resource is abstract and you can understand it as something that is identified by a URL provided by the server . REST资源的概念是抽象的 ,您可以将其理解为由服务器提供的URL标识的内容

The resource can be a user, a list of users, a customer, a file or any entity of the application. 资源可以是用户,用户列表,客户,文件或应用程序的任何实体。

For example, consider a user as your resource with the following attributes and values: 例如,将用户视为具有以下属性和值的资源:

  • ID: 1 ID:1
  • First name: John 名字:约翰
  • Last name: Doe 姓氏:Doe
  • e-mail: john.doe@example.com 电子邮件:john.doe@example.com

URL 网址

The URL ( Uniform Resource Locator ) just identifies the resource, that is, where the resource is located in the server. URL( 统一资源定位符仅标识资源,即资源在服务器中的位置。

For example, while the URL /app/users/1 locates the user with the ID 1 , the URL /app/users locate all the users in the application. 例如,当URL /app/users/1找到ID为1的用户时,URL /app/users找到应用程序中的所有用户。

HTTP methods HTTP方法

REST is protocol independent, but, if you are using HTTP, you can perform actions on the resource accessing the URL with HTTP methods, such as GET , POST , PUT and DELETE . REST与协议无关,但是,如果您使用HTTP,则可以使用HTTP方法对访问URL的资源执行操作,例如GETPOSTPUTDELETE

For example, when you perform a GET to the URL /app/users/1 , you'll obtain a representation for the user with the ID 1 . 例如,当您对URL /app/users/1执行GET时,您将获得ID为1的用户的表示。

Resource representation 资源代表

A resource can be represented in multiple formats, such as JSON, XML, YAML, etc. 资源可以用多种格式表示,例如JSON,XML,YAML等。

In JSON, the representation would be: 在JSON中,表示形式为:

{
    "id": 1,
    "firstName": "John",
    "lastName": "Doe",
    "email": "john.doe@example.com"
}

In XML, the representation would be the following: 在XML中,表示形式如下:

<user>
    <id>1</id>
    <firstName>John</firstName>
    <lastName>Doe</lastName>
    <email>john.doe@example.com</email>
</user>

Example 1 例1

Consider you are developing an application in JavaScript and the server can provide a representation of the resources as JSON and XML. 考虑您正在使用JavaScript开发应用程序,并且服务器可以将资源表示为JSON和XML。 It's easier to deal with JSON rather than XML in JavaScript applications. 在JavaScript应用程序中处理JSON而不是XML更容易。 So, you want the resources represented as JSON. 因此,您希望表示为JSON的资源。

To do it, when performing a GET to the URL /app/users/1 , you'll add the HTTP header Accept with the application/json value to tell the server the representation the client accepts. 为此,当对URL /app/users/1执行GET时,您将使用application/json值添加HTTP头Accept ,以告知服务器客户端接受的表示。

Consequently, the server will return the resource represented as JSON. 因此,服务器将返回表示为JSON的资源。 The response will contain the Content-Type header with the application/json value, indicating the content of the response is a JSON. 响应将包含带有application/json值的Content-Type标头,指示响应的内容是JSON。


Example 2 例2

Besides JSON and XML, for example, the resources can be represented as images or videos. 例如,除了JSON和XML之外,资源可以表示为图像或视频。

Consider a URL which locates the profile picture of a user: /app/users/1/profile-picture . 考虑一个用于查找用户个人资料图片的URL: /app/users/1/profile-picture

Depending the image type, the Content-Type of the response will be image/jpeg , image/png , image/gif , etc. 根据图像类型,响应的Content-Type将是image/jpegimage/pngimage/gif等。


This answer may also be insightful. 这个答案也可能很有见地。

Here are the words of the Godfather of REST, Roy T. Fielding (from his dissertation ) 以下是REST教父Roy T. Fielding的话(来自他的论文

§ 5.2.1.1 Resources and Resource Identifiers §5.2.1.1资源和资源标识符

The key abstraction of information in REST is a resource . REST中信息的关键抽象是一种资源 Any information that can be named can be a resource: a document or image, a temporal service (eg “today's weather in Los Angeles”), a collection of other resources, a non-virtual object (eg a person), and so on. 可以命名的任何信息都可以是资源:文档或图像,临时服务(例如“洛杉矶的今天天气”),其他资源的集合,非虚拟对象(例如人)等等。 In other words, any concept that might be the target of an author's hypertext reference must fit within the definition of a resource. 换句话说,任何可能是作者超文本引用目标的概念都必须符合资源的定义。 A resource is a conceptual mapping to a set of entities, not the entity that corresponds to the mapping at any particular point in time... (There's more, click the link to read on). 资源是对一组实体的概念映射,而不是与任何特定时间点的映射相对应的实体...(还有更多,请单击要阅读的链接)。

Really a resource can be abstract concept that that is identifiable by a URI and can be represented in transmittable data. 实际上,资源可以是抽象概念,可由URI识别并且可以在可传输数据中表示。

§ 5.2.1.2 Representations §5.2.1.2陈述

REST components perform actions on a resource by using a representation to capture the current or intended state of that resource and transferring that representation between components. REST组件通过使用表示来捕获资源的当前或预期状态并在组件之间传输该表示,从而对资源执行操作。 A representation is a sequence of bytes, plus representation metadata to describe those bytes. 表示是字节序列,加上用于描述这些字节的表示元数据 Other commonly used but less precise names for a representation include: document, file, and HTTP message entity, instance, or variant. 其他常用但不太精确的表示名称包括:文档,文件和HTTP消息实体,实例或变体。

[...] [...]

The data format of a representation is known as a media type . 表示的数据格式称为媒体类型 A representation can be included in a message and processed by the recipient according to the control data of the message and the nature of the media type. 表示可以包括在消息中并由接收者根据消息的控制数据和媒体类型的性质进行处理。

So really the representation is the data (or state) that represents the resource. 实际上,表示是表示资源的数据 (或状态)。

The data format of the representation is formally the media type, but you may also hear it being called the variant. 表示的数据格式正式是媒体类型,但您也可以听到它被称为变体。

Everyone interested in REST, should at least read Chapter 5 of that dissertation. 每个对REST感兴趣的人至少应该阅读该论文的第5章。 After all, it's how REST came to be. 毕竟,这就是REST的结果。

With REST all depends on what representation (ie MIME format) you are requesting in your HTTP GET. 使用REST,所有取决于您在HTTP GET中请求的表示形式(即MIME格式)。 There are eg HTTP headers to convey that. 有一些HTTP标头可以传达它。 From the client's perspective it is always about representation(s) prior to the "raw" data. 从客户的角度来看,它始终是关于“原始”数据之前的表示。

The glory details are here . 荣耀的细节在这里

So yes, you will "get the whole xml or json 'object' that contain both the data and the data representation". 所以是的,你将“获得包含数据和数据表示的整个xml或json'对象”。

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

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