回答

收藏

RESTful 编程到底是什么?

技术问答 技术问答 259 人阅读 | 0 人回复 | 2023-09-11

RESTful 什么是编程?4 I/ c: V. T* D
                                                               
% k3 F. U7 O4 W, I& O+ N" I  Y    解决方案:                                                               
7 f5 [3 S/ }1 Z, B4 K' C1 k: X                                                                REST是 Web 底层结构原则。Web令人惊讶的是,客户端(浏览器)可以以复杂的方式与服务器交互,而客户端不需要事先了解服务器及其托管资源。关键约束是服务器和客户端必须就所使用的媒体达成协议Web 的情况是HTML。5 s( x% N2 ^9 U1 n
遵循REST原则的 API客户端不需要了解 API 结构。相反,服务器需要提供客户端与服务互动所需的任何信息。HTML表格就是这样一个例子:服务器指定资源和所需字段的位置。浏览器不知道在哪里提前提交信息,也不知道提前提交什么信息。服务器完全提供两种信息。(这个原理叫HATEOAS : Hypermedia As The Engine Of Application State。)
) C3 a% |0 C0 P- j' k  |那么,如何应用这个呢?HTTP,如何在实践中实现?HTTP以动词和资源为导向。主流用法中的两个动词是GETand POST,我想大家都知道。HTTP标准定义了其他几个标准,例如PUT和DELETE。然后根据服务器提供的指令将这些动词应用于资源。
: [! |; K) [2 S) N  F例如,假设我们有 Web 服务管理用户数据库。我们的服务是基于 JSON 自定义超媒体,我们分配 mimetype application/json userdb(也可能有一个application/xml userdb和application/whatever userdb-可能支持多种媒体类型)。客户端和服务器都被编程来理解,但他们对彼此一无所知。正如罗伊菲尔丁所指出的:$ ]2 i' p" b/ B) ~' q$ n8 k
REST API 几乎所有的描述性工作都应该用来定义用于表示资源和驱动应用程序状态的媒体类型,或者定义扩展关系名称和/或现有标准媒体类型的超文本启用标志。
) G1 b3 T3 [$ B6 h. h5 X; i要求基础资源/可返回以下内容:4 s+ t4 W/ F% `
要求( j2 s$ g* f) ]* F% E0 d
GET /Accept: application/json userdb回复
9 I: z0 T. n8 b( T200 OKContent-Type: application/json userdb{    "version": "1.0",   "links":                              "href": "/user",           "rel": "list",           "method": "GET"        "href": "/user",           "rel": "create",           "method": &quotOST"       我们从媒体的描述中知道,我们可以从被称为链接的部分中找到相关资源的信息。这被称为超级媒体控制器。在这种情况下,我们可以从这些部分了解到,我们可以通过发送另一个请求来找到用户列表/user:
4 W& g6 A( U! Q# Z7 C3 \要求4 @, T  _& J% V' ?
GET /userAccept: application/json userdb回复; |: M, p/ S& ^) j1 D- c1 F7 c- S3 W1 M
200 OKContent-Type: application/json userdb{    "users":                              "id": 1、          "name": "Emil",           "country: "Sweden",           "links":                                                                "href": "/user/1",                   "rel": "self",                   "method": "GET"                                                                    "href": "/user/1",                   "rel": "edit",                   "method": &quotUT"                },                                                         "href": "/user/1",                   "rel": "delete",                   "method": "DELETE"                                        "id": 2、          "name": "Adam",           "country: "Scotland",           "links": [                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         "href": "/user/2",                   "rel": "self",                   "method": "GET"                },                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        "href": "/user/2",                                                                            "rel": "edit",                   "method": &quotUT"            "href": "/user/2",                   "rel": "delete",                   "method": "DELETE"                "links":                              "href": "/user",                                            "rel": "create",           "method": &quotOST"       我们可以从这个回应中看到很多。例如,我们现在知道我们可以通过POSTing 创建新用户/user:5 ^; H5 n* M% Y
要求
/ p5 ~7 C3 W& rPOST /userAccept: application/json userdbContent-Type: application/json userdb{    "name": "Karl",   "country": "Austria"}回复' b4 u4 R9 u( W* K; ]0 R- q" |( h
201 CreatedContent-Type: application/json userdb{    "user":              "id": 3、        "name": "Karl",       "country": "Austria",       "links": [                                 "href": "/user/3",               "rel": "self",               "method": "GET"            },                                "href": "/user/3",    "method": "GET"         "method": "GET"    }}我们也知道我们可以更改现有数据:
. ?. Y; Y4 s" w' J1 r) q要求
6 U" F3 \  r# }1 `2 ]3 y! LPUT /user/1Accept: application/json userdbContent-Type: application/json userdb{    "name": "Emil",   "country": "Bhutan"}回复
" l, B. y8 o, d* U8 C7 u: G0 Q+ O# a200 OKContent-Type: application/json userdb{    "user":              "id": 1、         "name": "Emil",       "country": "Bhutan",       "links": [                                 "href": "/user/1",               "rel": "self",               "method": "GET"            },                                "href": "/user/1",    "rel": "edit",               "method": &quotUT"          "href": "/user/1",               "rel": "delete",               "method": "DELETE"          "links":          "href": "/user",      "rel": "list",      "method": "GET"    }}请注意,我们使用不同的方法HTTP动词(GET,PUT,POST,DELETE等)来操纵这些资源,我们假设只有知识是我们媒体对客户端的定义。
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则