API开发者永不“REST”

{"type":"doc","content":[{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic"}],"text":"免责声明:尽管标题有争议,但本文并不是试图证明RPC比REST更好,或者GraphQL比RPC更好。相反,本文的目的是向你介绍这些方法的大致情况以及它们的优缺点。最终的选择将会是一个权衡。"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"尽管HTTP是一个应用层(例如,L7)协议,但在API开发方面,HTTP实际上扮演着一个较低层次的传输机制的角色。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在HTTP上如何实现API有许多方法,它们在概念上有所不同:"}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"REST"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"RPC"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"GraphQL"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"...但是一个普通的开发人员需要知道的实际清单并不局限于这三个家伙。在这个领域中还有JSON、gRPC、protobuf等其它术语。让我们试着一次性了解所有这些术语吧!"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.infoq.cn\/resource\/image\/6c\/be\/6cc50627f894c926c66fae8yybafb0be.png","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"代表性状态转移(REST)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"首先,"},{"type":"link","attrs":{"href":"https:\/\/en.wikipedia.org\/wiki\/Representational_state_transfer","title":"","type":null},"content":[{"type":"text","text":"REST"}]},{"type":"text","text":"只是一种软件架构风格。它是一组设计约束,而不是具体的协议。REST依赖于资源的概念。例如,一个REST API是由一组资源(名词)和与这些资源交互的有限数量的动作(动词,查询fetch、创建create、更新update、删除delete等)组成。它在思想上非常接近最初的HTTP设计,主要基于资源(URLs)和方法(GET、POST、PUT、DELETE)。因此,从实现的角度来看,REST模型到HTTP协议的映射相对简单:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"# Create new book\nPOST http:\/\/myapi.com\/books\/ (author=\"R. Feynman\", year=1975) -> book_id\n\n\n# Get book with ID = 1\nGET http:\/\/myapi.com\/books\/1 () -> (id=1, author=\"R. Feynman\", year=1975)\n\n\n# Update book with ID = 1\nPUT http:\/\/myapi.com\/books\/1 (id=1, author=\"Richard Feynman\", year=1975) -> (...)\n\n\n# Delete book with ID = 1\nDELETE http:\/\/myapi.com\/books\/1 () -> nu"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"可能每一个现代web框架都提供了构建一个REST风格的Web服务所需的所有现成工具。从客户端的角度来看,调用一个REST API非常简单——它只需要将指定的HTTP方法发送到一组预定义的URLs。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章