什么是“表示",“状态"?和“转移"在代表性状态转移(REST)中? [英] What is "representation", "state" and "transfer" in Representational State Transfer (REST)?

查看:202
本文介绍了什么是“表示",“状态"?和“转移"在代表性状态转移(REST)中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一些有关REST的资源,但是我不清楚.如果有人可以在下面的示例中解释一些事情,将会对我有帮助.

I came across a few resources regarding REST but I'm not able to understand things clearly. It would help me if someone could explain things with respect to my example below.

我有一个名为User

User表内容

id name
1  xxx

我要呼叫的URL是/test/1

结果将为JSON格式,例如:{ 1: "xxx" }

The result will be in JSON format, eg: { 1: "xxx" }

到目前为止,我对REST的理解是

My understanding so far regarding REST:

  • 资源-用户表内容
  • 表示形式-表/JSON
  • 状态转移-表格形式的数据到JSON.
  • Resource - User table content
  • Representation - table/JSON
  • State transfer - data in the form of table to JSON.

如果我的理解正确,请告诉我. 否则,请回答以下问题:

Please do let me know if my understanding is correct. Else, please answer the below questions:

  • 在我的示例中什么是资源?
  • 在我的示例中什么是表示形式?
  • 什么是状态转移?在我的示例中何时发生?

推荐答案

REST是通过在客户端与服务器之间的无状态通信之上通过其表示进行资源状态操纵的.这是一种与协议无关的体系结构样式,但实际上,它通常是在HTTP协议的顶部实现的.

REST is about resource state manipulation through their representations on the top of stateless communication between client and server. It's a protocol independent architectural style but, in practice, it's commonly implemented on the top of the HTTP protocol.

在设计基于HTTP的REST时,URL用于定位资源,HTTP方法用于表示对资源的操作,并且诸如JSON和/或XML文档的表示用于表示资源的状态. HTTP标头可用于交换有关请求和响应的某些元数据,而HTTP状态码则用于通知客户端有关操作状态的信息.

When designing REST over HTTP, URLs are used to locate the resources, HTTP methods are used to express the operations over the resources and representations such as JSON and/or XML documents are used to represent the state of the resource. HTTP headers can be used to exchange some metadata about the request and response while HTTP status code are used to inform the client regarding the status of the operation.

在我的示例中,什么是资源?

资源理解为用户的概念.不要考虑数据库中的表,而应该考虑用户及其属性集的抽象.

Understand resource as the concept of a user. Don't think about the table in your database, think about an abstraction of a user with their set of attributes.

在我的示例中,什么是表示形式?

JSON文档可用于表示特定资源的状态.资源可以具有许多表示形式,例如JSON和/或XML文档,并且客户端可以使用内容协商来请求同一资源的不同表示形式.

A JSON document can be used to represent the state of a particular resource. A resource can have many representations, such as JSON and/or XML documents, and the client can use content negotiation to request different representations of the same resource.

什么是状态转移?或者在我的示例中何时发生?

What is a state transfer or when does this happens in my example?

可以使用表示来检索和操纵给定资源的状态.

The state of a given resource can be retrieved and manipulated using representations.

例如,GET请求允许您检索在响应有效负载中发送的资源状态的表示形式.例如,PUT请求允许您将资源的状态替换为请求有效负载中包含的表示形式所定义的状态.

A GET request, for example, allows you to retrieve a representation of the state of a resource, sent in the response payload. A PUT request, for example, allows you to replace the state of a resource with the state defined by the representation enclosed in the request payload.

考虑具有以某种方式存储在服务器中的诸如 id name 之类的属性的用户资源:

Consider a user resource with attributes such as id and name stored somehow in your server:

  • ID:1
  • 姓名:John Doe

这些详细信息使资源成为状态.

These details make the state of the resource.

诸如/users/1之类的URL可用于定位服务器中的资源.

A URL such as /users/1 can be used to locate the resource in your server.

GETPUTDELETE之类的请求,以使用表示(例如JSON和/或XML文档)来检索/操纵资源的状态. (可以根据您的需要支持其他表示形式):

Requests such as GET, PUT and DELETE can be performed against this URL to retrieve/manipulate the state of the resource using representations, such as JSON and/or XML documents (other representations can be supported according to your needs):

{
  "id": 1,
  "name": "John Doe"
}

<user>
  <id>1</id>
  <name>John Doe</name>
</user>

上面显示的文档不是资源本身.它们只是表示资源的一种方式.它以某种方式存储在您的服务器中.

The above shown documents are not the resource itself. They are just a way to represent the resource. which is stored somehow in your server.

这篇关于什么是“表示",“状态"?和“转移"在代表性状态转移(REST)中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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