Java REST 如何“轻量级”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20088759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How REST is "lightweight"?
提问by Manoj
I have been seeing SOAP is "Heavy Weight" and REST is "Light Weight". On what parameters, we are telling REST is lightweight than SOAP?
我一直看到 SOAP 是“重量级”而 REST 是“重量级”。在哪些参数上,我们告诉 REST 比 SOAP 轻量级?
We were using IFW model web services in our company earlier. But our management told us to develop all the new APIs going forward in REST. We are backend service providers in my company.
早些时候,我们在公司中使用了 IFW 模型 Web 服务。但是我们的管理层告诉我们要在 REST 中开发所有新的 API。我们是我公司的后端服务提供商。
How REST is best useful for us?
REST 如何对我们最有用?
What does "lightweight" means in context?
“轻量级”在上下文中是什么意思?
This question seems like repetition but don't understand the terms used.
这个问题似乎是重复,但不明白使用的术语。
回答by midhunhk
REST is lightweight in that it and relies upon the HTTP standard to do it's work. It is great to get a useful web service up and running quickly. If you don't need a strict API definition, this is the way to go. Most web services fall into this category. You can version your API so that updates to the API do not break it for people using old versions(as long as they specify a version). REST essentially requires HTTP, and is format-agnostic, so you can use XML, JSON, HTML etc).
REST 是轻量级的,它依赖于 HTTP 标准来完成它的工作。快速启动并运行有用的 Web 服务非常棒。如果您不需要严格的 API 定义,这就是要走的路。大多数 Web 服务都属于这一类。您可以对 API 进行版本控制,以便对 API 的更新不会破坏使用旧版本的人(只要他们指定版本)。REST 本质上需要 HTTP,并且与格式无关,因此您可以使用 XML、JSON、HTML 等)。
But the SOAP will wrap the structure into SOAP envelope (follows an XML standard). The complexity of SOAP envelope is based on used message version and additional Web Service protocols. SOAP is generally transport-agnostic, meaning you don't necessarily need to use HTTP.
但是 SOAP 会将结构包装到 SOAP 信封中(遵循 XML 标准)。SOAP 信封的复杂性基于使用的消息版本和附加的 Web 服务协议。SOAP 通常与传输无关,这意味着您不一定需要使用 HTTP。
It can be pointed out that SOAP was designed for a distributed computing environment where as REST was designed for a point to point environment.
可以指出,SOAP 是为分布式计算环境设计的,而 REST 是为点对点环境设计的。
I wonder if it is OK to say that while SOAP gives more security, REST based API s will be easier on the resources and more scalable? As an example Twitter, Facebook, Google Drive, Blogger etc all have REST based APIs that clients can consume.
我想知道,虽然 SOAP 提供了更高的安全性,但基于 REST 的 API 在资源上更容易并且更具可扩展性,这样说是否可以?例如,Twitter、Facebook、Google Drive、Blogger 等都有客户端可以使用的基于 REST 的 API。
Check out this linkfor a comparison on SOAP and REST.
查看此链接以比较 SOAP 和 REST。
回答by Elliott Frisch
Generally REST
reduces the size and scope of request payloads by mapping request semantics to the underlying HTTP protocol. For example, SOAP
will usually add an envelope (of varying complexity) which utilizes a WSDL
(a contract) for both request and response types and service mappings. REST will just use POST, GET, etcto a URL with some HTTP encoded parameters and thus lacks an enforced contract.
通常REST
通过将请求语义映射到底层 HTTP 协议来减少请求有效负载的大小和范围。例如,SOAP
通常会添加一个信封(具有不同的复杂性),它WSDL
为请求和响应类型以及服务映射使用(一个合同)。REST 只会对带有一些 HTTP 编码参数的 URL使用POST、GET 等,因此缺乏强制合同。
回答by Slihp
REST gives you a session less window into a system. It does not track you, it does not care about you. All you have done is send a request which contains..hopefully some id to verify that you can make it. It may return a HTTP status code, it may return some body but ultimately, once the request is complete you are forgotten.
REST 为您提供了进入系统的无会话窗口。它不会跟踪您,也不会关心您。您所做的就是发送一个请求,其中包含……希望有一些 id 来验证您是否可以做到。它可能会返回一个 HTTP 状态代码,它可能会返回一些正文,但最终,一旦请求完成,您就会被遗忘。
SOAP is heavy in a sense that it describes a "contract" between the remote system and your client. In order for your client to communicate effectively it MUST implement its schema...this is the SOAP skeleton. It describes the calls you can make and the objects you can expect back.
从某种意义上说,SOAP 很重,它描述了远程系统和您的客户端之间的“契约”。为了让您的客户端有效地进行通信,它必须实现其架构……这是 SOAP 框架。它描述了您可以进行的调用以及您可以期待返回的对象。
The reason why SOAP is heavy is because of serialization. Upon each SOAP request you typically serialize a java object, send it over HTTP and get a serialized response which is deserialized into an object via reflection...this is heavy. It also means that if the endpoint changes how they work, you must change your contract. You don't have to do this with REST.
SOAP之所以重,是因为序列化。在每个 SOAP 请求上,您通常会序列化一个 java 对象,通过 HTTP 发送它并获得一个序列化的响应,该响应通过反射反序列化为一个对象......这很重。这也意味着如果端点改变了它们的工作方式,你必须改变你的合同。您不必使用 REST 来执行此操作。
With SOAP you run into multi threaded issues.
使用 SOAP 您会遇到多线程问题。
To answer quickly..
要快速回答..
they might mean that a REST service is "lightweight" because you do not need to release changes to clients. You simply make changes to your logic, retaining URLS and the response should remain the same.
它们可能意味着 REST 服务是“轻量级的”,因为您不需要向客户端发布更改。您只需更改逻辑,保留 URL,响应应保持不变。
With SOAP...if you added a new field to an object, you would have to get the client to implement the new schema. SOAP is pretty old had.
使用 SOAP...如果向对象添加新字段,则必须让客户端实现新模式。SOAP 已经很老了。
回答by Petter Nordlander
There are several aspects of lightweight. SOAP is XML only, while REST allows you to send any payload, such as JSON which is less verbose and simpler than XML. Generally speaking, it takes less memory and less bandwidth to deal with JSON than XML.
轻量化有几个方面。SOAP 仅是 XML,而 REST 允许您发送任何有效负载,例如 JSON,它比 XML 更简洁、更简洁。一般来说,与 XML 相比,处理 JSON 需要更少的内存和更少的带宽。
On another level, using SOAP you typically describe everything as services. So, you need to define a message schema/structure for each verb and noun i.e. "CreateOrder". In REST, you use the predefined HTTP methods such as POST and put the noun/"resource" in the URI which uses already existing constructs on the HTTP level - not reinventing them in XML.
在另一个层面上,使用 SOAP 您通常将一切都描述为服务。因此,您需要为每个动词和名词(即“CreateOrder”)定义一个消息模式/结构。在 REST 中,您使用预定义的 HTTP 方法(例如 POST)并将名词/“资源”放在 URI 中,该 URI 使用 HTTP 级别上的现有构造 - 而不是在 XML 中重新发明它们。
Then there is the effort it takes to adapt to change. A small change in SOAP requires you to redefine the schema, redefine the wsdl and handle versions. In rest, in the best of worlds, there is a degree of dynamic coded into the API (links to referenced resources, among other things), so that some changes can be implemented and takes effect directly.
然后是适应变化所需的努力。SOAP 中的一个小变化需要您重新定义模式、重新定义 wsdl 和句柄版本。在其他方面,在最好的情况下,API 中有一定程度的动态编码(指向引用资源的链接等),以便可以实施某些更改并直接生效。
All the heavy weight strictness of SOAP of course has a valid reason for existence. It's less likely two implementations have different interpretations of the API, than in a loosely defined REST API. SOAP also allows you to implement skeleton code in various languages based on a WSDL. In some complex scenarios, this is actually almost the only productive way to work with SOAP. Marshalling and unmarshalling between objects and serialized XML is rather heavy and adds to the heavyness of SOAP.
SOAP 的所有重量级严格性当然都有存在的正当理由。与松散定义的 REST API 相比,两种实现对 API 的解释不同的可能性较小。SOAP 还允许您基于 WSDL 以各种语言实现框架代码。在一些复杂的场景中,这实际上几乎是使用 SOAP 的唯一有效方式。对象和序列化 XML 之间的编组和解组相当繁重,并且增加了 SOAP 的繁重性。