简体   繁体   English

REST api 和 REST 服务器有什么区别

[英]What is difference between REST api and REST server

I am a little bit confused about this.我对此有点困惑。 Can I call my Java server REST server or should I call it REST api?我可以调用我的 Java 服务器 REST 服务器还是应该调用它 REST Z8A5DA52ED1264417D35AZ7E? What is the right terminology of this?什么是正确的术语? When can you call something REST api and when REST server?你什么时候可以打电话给 REST api 什么时候打电话给 REST 服务器?

Thanks谢谢

In simple terms a REST API is a set of URL s that respond to requests made over HTTP , usually using GET POST PUT DELETE HTTP methods. In simple terms a REST API is a set of URL s that respond to requests made over HTTP , usually using GET POST PUT DELETE HTTP methods. Lots of REST API s return JSON in the response.许多REST API在响应中返回JSON

For example, to get a customer's details a REST API might be a GET request to:例如,要获取客户的详细信息, REST API可能是一个GET请求:

https://customers.com/api/1234

which responds with:回应:

{
  "id": 1234,
  "name": "Joe Bloggs"
}

The REST API part is /api/1234 . REST API部分是/api/1234 An example of a framework that can be used to simplify creation of a REST API is spring-boot .可用于简化REST API的创建的框架示例是spring-boot

The REST Server part is https://customers.com REST 服务器部分是https://customers.com

ie The REST Server is there to provide the infrastructure to allow clients to send GET requests to the REST API and receive the response.REST Server提供基础设施,允许客户端向REST API发送GET请求并接收响应。

Examples of servers that can be used as REST Servers are Apache HTTPD , Tomcat , IIS etc.可用作 REST 服务器的示例有Apache HTTPDTomcatZ52766ACF461B5ECB27等。

To answer some questions:回答一些问题:

I should use REST API instead of server我应该使用 REST API 而不是服务器

A REST API cannot be used without a Rest Server . REST API不能在没有Rest Server的情况下使用。 The server is the application that accepts requests to the API and facilitates responses from the API .服务器是接受对API的请求并促进来自API的响应的应用程序。 The REST API client will send a GET request to the REST Server for customer.com/api/1234 . REST API客户端将为customer.com/api/1234REST Server发送GET请求。 The REST Server will work out that 'customer.com/api/1234' is a web application running inside the REST Server and will pass control to that web application. The REST Server will work out that 'customer.com/api/1234' is a web application running inside the REST Server and will pass control to that web application.

The answer to the next question follows on from that hand-over:下一个问题的答案是从那次交接开始的:

REST API is divided into three tiers - presentation, business and data REST API分为三层——表现层、业务层和数据层

That is entirely up to the developer of the REST API .这完全取决于REST API的开发人员。 That is the implementation detail of the REST API .这是REST API的实现细节。 For example, a typical flow might be:例如,典型的流程可能是:

  1. REST Server receives GET request for customer.com/api/1234 REST Server接收到customer.com/api/1234GET请求
  2. REST Server hands control to REST API which receives the URL path parameter 1234 REST Server将控制权交给REST API接收URL路径1234
  3. REST API determines request is for a customer (business tier) REST API确定请求是针对客户(业务层)
  4. REST API contacts database to load data of the customer with id 1234 (data tier) REST API联系数据库以加载 id 为 1234 的客户的数据(数据层)
  5. REST API returns JSON as shown above (presentation tier) REST API返回JSON如上所示(表示层)
  6. REST Server sends JSON response to the client REST Server向客户端发送JSON响应

So all domain operations are handled by the REST API (finding a customer, converting the data to JSON) and all internet operations are handled by the REST Server (client connections, HTTP requests and responses). So all domain operations are handled by the REST API (finding a customer, converting the data to JSON) and all internet operations are handled by the REST Server (client connections, HTTP requests and responses).

In the spring-boot framework, you can develop your REST API using Java and also bundle it with a built-in REST Server (Tomcat) so you only produce a single JAR file that you place on a computer and run. In the spring-boot framework, you can develop your REST API using Java and also bundle it with a built-in REST Server (Tomcat) so you only produce a single JAR file that you place on a computer and run.

So in effect, you have three components.所以实际上,你有三个组件。 The computer (for example a unix server connected to the internet, or even your pc and use http://localhost/customer/api/1234 ).计算机(例如连接到 Internet 的 unix 服务器,甚至您的 pc 并使用http://localhost/customer/api/1234 )。 A REST Server (Tomcat, that can accept HTTP requests) and a REST API (the code you wrote to implement the tiers). A REST Server (Tomcat, that can accept HTTP requests) and a REST API (the code you wrote to implement the tiers).

An API is an Application Programming Interface, which is a way to provide information for other applications (communication among applications). API 是一个应用程序编程接口,它是一种为其他应用程序(应用程序之间的通信)提供信息的方式。 A server is any machine running some process that will execute some service for you.服务器是运行某些进程的任何机器,它将为您执行某些服务。

In other words, however you call it, the important part is that when talking with programmers about this, they will call it API because it's the part related to the code.换句话说,不管你怎么称呼它,重要的部分是当与程序员谈论这个时,他们会称之为API,因为它是与代码相关的部分。 If you call it server, others may understand that you're talking about the machine itself, not the application running on it.如果您将其称为服务器,其他人可能会理解您是在谈论机器本身,而不是在其上运行的应用程序。

(This is my vision as a programmer in a big Enterprise and very often I see IT people misunderstand programmers because of these small concepts) (这是我作为大企业程序员的愿景,我经常看到 IT 人员因为这些小概念而误解程序员)

REST is actually an architectural style and according to Robert C. REST 实际上是一种建筑风格,根据罗伯特 C 的说法。 "Uncle Bob" Martin an architecture is about intent . “鲍勃叔叔”马丁的架构是关于意图的 The intent behind REST is the decoupling of clients from servers to allow the latter ones to evolve freely in future without having to fear breaking clients. REST 背后的意图是将客户端与服务器分离,以允许后者在未来自由发展,而不必担心破坏客户端。 In order to achieve decoupling clients and servers have to adhere to a certain set of constraints.为了实现解耦,客户端和服务器必须遵守一定的约束。

REST can't be reduced therefore to the server side alone. REST 不能单独减少到服务器端。 It is the whole interaction characteristic or behavior between client and server that determine whether a distributed system follows a REST architectural design or not.决定分布式系统是否遵循 REST 架构设计的是客户端和服务器之间的整体交互特征或行为。 If you will you might tackle it from a SOA perspective and say that a server offers services to clients.如果您愿意,您可能会从 SOA 的角度来处理它,并说服务器向客户端提供服务。 Even if you have a service implemented that adheres to all constraint put up by Fielding, the whole interaction between server and clients might not be "RESTful" if clients rely on identifying semantics from URIs or expect certain endpoints to return certain types instead of relying on content-type negotiation or implement other kinds of couplings to a respective server.即使您实现的服务遵守 Fielding 提出的所有约束,如果客户端依赖于从 URI 识别语义或期望某些端点返回某些类型而不是依赖于内容类型协商或实现与相应服务器的其他类型的耦合。

Jim Webber pointed out that in a REST architecture you primarily implement a domain application protocol that client will follow along as they get all the information served by the server, either through links or form-like representation similar to HTML forms. Jim Webber指出,在 REST 架构中,您主要实现一个域应用程序协议,当客户端通过链接或类似于 HTML ZAC68B62ABFD6A9FE26E8AC423ZCECE8 的类似形式的表示获取服务器提供的所有信息时,客户端将遵循该协议。 This concept is summarized as HATEOAS.这个概念被概括为 HATEOAS。 HTTP, further, is a transport protocol whose domain is the transfer of documents across the Web.此外,HTTP 是一种传输协议,其域是跨 Web 的文档传输。 You don't invoke services, you just shovel around documents.你不调用服务,你只是在文件周围铲。 Any business rules you conclude from the file transmissions are just a side effect of the actual document management.您从文件传输中得出的任何业务规则只是实际文档管理的副作用。 Thereby invoking a REST service is probably also not the correct term actually.因此调用 REST 服务实际上也可能不是正确的术语。

A REST API in itself is misleading in the REST ecosystem as REST is defined to reuse the common interface provided by its transport layer, HTTP in most cases, but it is not limited to it actually. A REST API in itself is misleading in the REST ecosystem as REST is defined to reuse the common interface provided by its transport layer, HTTP in most cases, but it is not limited to it actually. Here HTTP ie is the common interface both clients and servers use and neither server nor clients should implement customization to it that may cause interoperability issues.这里 HTTP 即是客户端和服务器使用的通用接口,服务器和客户端都不应该对其进行自定义,这可能会导致互操作性问题。 The ultimate goal in a REST environment is that one client may interact with a plethora of services out of the box while a server may serve plenty of different clients, especially ones not under the developers own control, without the need for external documentation and customization overhead, except for the integration of standard document formats, such as HTML or other hypertext driven media-type formats, and link-relations. REST 环境的最终目标是一个客户端可以与大量开箱即用的服务进行交互,而服务器可以为大量不同的客户端提供服务,尤其是那些不受开发人员控制的客户端,而无需外部文档和定制开销,除了标准文档格式的集成,例如 HTML 或其他超文本驱动的媒体类型格式和链接关系。 The coupling should not be between client and servers but between a peer (server or client) and the negotiated representation format defined by a standardized media-type, though through proper content-type negotiation both server and client agreed on a representation format both support and understand.耦合不应该在客户端和服务器之间,而是在对等点(服务器或客户端)和由标准化媒体类型定义的协商表示格式之间,尽管通过适当的内容类型协商,服务器和客户端都同意表示格式既支持和理解。

There is unfortunately a widespread confusion of what REST really is.不幸的是,人们普遍混淆了 REST 到底是什么。 If you look here at SO or on the Web in general you might get the impression that REST means exposing arbitrary JSON payloads via over-engineered URLs on some HTTP endpoints. If you look here at SO or on the Web in general you might get the impression that REST means exposing arbitrary JSON payloads via over-engineered URLs on some HTTP endpoints. Such systems behave like true RPC APIs, similar to SOAP or CORBA.此类系统的行为类似于真正的 RPC API,类似于 SOAP 或 CORBA。 They ship with their own documentation or type definitions that allow de/serialization of messages, clients will usually break if something changes in the structure and similar stuff, and clients targeting one of these APIs can't usually be reused for other APIs out of the box.他们附带自己的文档或类型定义,允许对消息进行反序列化/序列化,如果结构和类似内容发生变化,客户端通常会中断,并且针对这些 API 之一的客户端通常不能被其他 API 重用盒子。 These are strong hints for coupling and RPC-like behavior.这些是耦合和类 RPC 行为的有力提示。 Such "services" need to document the "API" so other developers can implement clients that are able to interact with those systems.此类“服务”需要记录“API”,以便其他开发人员可以实现能够与这些系统交互的客户端。 As clients demand such documentation, the documentation becomes the actual truth the server implementation has to follow otherwise clients might stop working.当客户端需要此类文档时,文档成为服务器实现必须遵循的实际事实,否则客户端可能会停止工作。 Such a coupling also means that a service can't evolve freely in future as it might break clients due to the tight coupling between API documentation and implementation.这种耦合也意味着服务在未来不能自由发展,因为 API 文档和实现之间的紧密耦合可能会破坏客户端。

As you hopefully can see by yourself, the term API is a bit risky in general if you talk about the true REST architecture model as proposed by Fielding.正如您希望自己看到的那样,如果您谈论 Fielding 提出的真正的 REST 架构 model,那么术语 API 通常有点冒险。 If you want to communicate about the thing most developers consider being REST, but is RPC actually, the term API might be more fitting.如果您想交流大多数开发人员认为是 REST 但实际上是 RPC 的东西,那么术语 API 可能更合适。 IMO the term "service" covers the thing exposed by server more properly as it covers both definitions accurately. IMO 术语“服务”更准确地涵盖了服务器公开的内容,因为它准确地涵盖了这两个定义。

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

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