gpt4 book ai didi

java - 我们应该在什么情况下使用 SOAP,什么时候应该使用 REST?

转载 作者:行者123 更新时间:2023-12-02 09:04:11 26 4
gpt4 key购买 nike

什么时候应该使用 SOAP,什么时候应该使用 REST?有人可以对此给出合理的答案吗?

这是在采访时被问到的。我说这取决于与其他方签订的契约(Contract)。我不知道这是对还是错。有人可以帮忙解决这个问题吗?

SOAP 我的意思是像下面这样的结构。

    <?xml version='1.0' Encoding='UTF-8' ?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">
<env:Header>
<m:reservation xmlns:m="http://travelcompany.example.org/reservation"
env:role="http://www.w3.org/2003/05/soap-envelope/role/next">
<m:reference>uuid:093a2da1-q345-739r-ba5d-pqff98fe8j7d</m:reference>
<m:dateAndTime>2007-11-29T13:20:00.000-05:00</m:dateAndTime>
</m:reservation>
<n:passenger xmlns:n="http://mycompany.example.com/employees"
env:role="http://www.w3.org/2003/05/soap-envelope/role/next">
<n:name>Fred Bloggs</n:name>
</n:passenger>
</env:Header>
<env:Body>
<p:itinerary xmlns:p="http://travelcompany.example.org/reservation/travel">
<p:departure>
<p:departing>New York</p:departing>
<p:arriving>Los Angeles</p:arriving>
<p:departureDate>2007-12-14</p:departureDate>
<p:departureTime>late afternoon</p:departureTime>
<p:seatPreference>aisle</p:seatPreference>
</p:departure>
<p:return>
<p:departing>Los Angeles</p:departing>
<p:arriving>New York</p:arriving>
<p:departureDate>2007-12-20</p:departureDate>
<p:departureTime>mid-morning</p:departureTime>
<p:seatPreference></p:seatPreference>
</p:return>
</p:itinerary>
</env:Body>
</env:Envelope>

最佳答案

简单来说,使用 REST,您可以描述与数据库查找、插入、更新、删除相同的操作。在 REST 中,此类操作与 HTTP 命令配对:GET、PUT、POST、DELETE。这也是为什么 REST 被称为“状态转移”,因为您可以像在数据库中一样将对象移入/移出,换句话说就是更改“模型”的状态。

另一方面,SOAP 更多地反射(reflect)了远程调用过程 (RPC),其中消息大量由 XML 结构化,这意味着在 SOAP 中您不只是简单地传输数据,而是提交命令。在 SOAP 中,您可以像编程语言一样定义方法和参数。关于最后一点,为了允许客户端使用 SOAP 服务定义的“方法”(实际上是结构良好的 XML 消息,带有名称和参数),同一服务必须发布一个 XML 文档来描述它可以发送哪些消息及其格式。 Accept:如果该方法不存在,则会抛出“Service notsupported”之类的错误。

关于java - 我们应该在什么情况下使用 SOAP,什么时候应该使用 REST?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59958825/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com