简体   繁体   English

RESTful和SOAP Web服务在实践中有何不同?

[英]How do RESTful and SOAP Web Services differ in practice?

I am implementing web services for a PHP application and am trying to understand what both standard web services and RESTful web services have to offer. 我正在为PHP应用程序实现Web服务,并试图了解标准Web服务和RESTful Web服务所提供的内容。 My intent is to write wrapper code to abstract away the web service details so that developers can just "instantiate remote objects" and use them. 我的目的是编写包装器代码来抽象出Web服务的详细信息,以便开发人员可以“实例化远程对象”并使用它们。 Here are my thoughts, perhaps some of you could add your experience and expand this: 以下是我的想法,或许你们中的一些人可以添加你的经验并扩展这个:

RESTful Web Servcies RESTful Web服务

are basically just "XML feeds on demand", so eg you could write wrapper code for a client application so it could query the server application in this way: 基本上只是“按需提供XML源”,因此您可以为客户端应用程序编写包装器代码,以便它可以通过以下方式查询服务器应用程序:

$users = Users::getUsers("state = 'CO'");
  • this would in turn get an XML feed form a remote URL 这将反过来从远程URL获取XML提要
  • $users could be made into a collection of full User objects, or $ users可以被制作成完整User用户对象的集合,或者
  • left as XML, or 保留为XML,或
  • turned into an array, etc. 变成阵列等
  • the query script ("state = 'CO'") would be translated into SQL on the server side 查询脚本(“state ='CO'”)将在服务器端转换为SQL
  • RESTful Web Services are in general read-only from the view of the client, although you could write code which could use POST or GET to make changes on the server, eg passing an encrypted token for security, eg: RESTful Web服务通常只能从客户端的视图中读取,尽管您可以编写可以使用POST或GET在服务器上进行更改的代码,例如传递加密令牌以确保安全性,例如:

    $users = Users::addUser($encryptedTrustToken, 'jim',$encryptedPassword, 'James', 'Taylor'); $ users = Users :: addUser($ encryptedTrustToken,'jim',$ encryptedPassword,'James','Taylor');

and this would create a new user on the server application. 这将在服务器应用程序上创建一个新用户。

Standard Web Services 标准Web服务

Standard Web Servcies in the end basically do the same thing. 标准Web服务最终基本上做同样的事情。 The one advantage they have is that client can discover their details via WSDL. 他们的一个优势是客户可以通过WSDL发现他们的详细信息。 But other than that, if I want to write wrapper code which allows a developer to instantiate, edit and save objects remotely, I still need to implement the wrapper code. 但除此之外,如果我想编写允许开发人员远程实例化,编辑和保存对象的包装代码,我仍然需要实现包装器代码。 SOAP doesn't do any of that for me, it can do this: SOAP不会为我做任何事情,它可以做到这一点:

$soap = new nusoap_client('http://localhost/test/webservice_user.php?wsdl', true);
$user = $soap->getProxy(); 
$lastName = $user->lastName();

but if I want to edit and save: 但如果我想编辑并保存:

$user->setLastName('Jones');
$user->save();

then I need to eg handle all of the state on the server side, SOAP doesn't seem to hold that object on the server side for each client. 然后我需要例如处理服务器端的所有状态,SOAP似乎并不在每个客户端的服务器端保存该对象。

Perhaps there are limitations in the PHP SOAP implementation I am using (nusoap). 也许我正在使用的PHP SOAP实现存在限制(nusoap)。 Perhaps Java and .NET implementations do much more. 也许Java和.NET实现可以做得更多。

Will enjoy hearing your feedback to clear up some of these clouds. 将享受听取您的反馈意见,以清理其中的一些云。

They are different models... REST is data-centric , where-as SOAP is operation-centric . 它们是不同的模型...... REST是以数据为中心的 ,其中SOAP是以运营为中心的 ie with SOAP you tend to have discrete operations "SubmitOrder", etc; 即使用SOAP,您往往会有离散操作“SubmitOrder”等; but with REST you are typically a lot more fluid about how you are querying the data. 但是使用REST,您通常会更加关注如何查询数据。

SOAP also tends to be associated with a lot more complexity (which is sometimes necessary) - REST is back to POX etc, and YAGNI . SOAP往往与更复杂的关联(有时是必要的) - REST回到POX等,以及YAGNI


In terms of .NET, tools like "wsdl.exe" will give you a full client-side proxy library to represent a SOAP service (or "svcutil.exe" for a WCF service): 就.NET而言,像“wsdl.exe”这样的工具将为您提供一个完整的客户端代理库来表示SOAP服务(或者用于WCF服务的“svcutil.exe”):

var someResult = proxy.SubmitOrder(...);

For REST with .NET, I guess ADO.NET Data Services is the most obvious player. 对于REST with .NET,我想ADO.NET数据服务是最明显的玩家。 Here, the tooling (datasvcutil.exe) will give you a full client-side data-context with LINQ support. 在这里,工具(datasvcutil.exe)将为您提供具有LINQ支持的完整客户端数据上下文。 LINQ is .NET's relatively new way of forming complex queries. LINQ是.NET相对较新的形成复杂查询的方式。 So something like (with strong/static type checking and intellisense): 所以像(强/静态类型检查和intellisense):

var qry = from user in ctx.Users
          where user.State == 'CO'
          select user;

(this will be translated to/from the appropriate REST syntax for ADO.NET Data Services) (这将被转换为ADO.NET数据服务的适当REST语法/从中

I'm echoing what Marc Gravell mentioned. 我在回应Marc Gravell提到的内容。 When people ask me about REST (and they usually have an idea about SOAP and SOA), I will tell them REST = ROA as it is resource/data oriented, it's a different paradigm and therefore has different design concerns. 当人们向我询问REST(他们通常对SOAP和SOA有所了解)时,我会告诉他们REST = ROA,因为它是面向资源/数据的,它是一个不同的范例,因此有不同的设计问题。

For your case, if I'm reading you correctly, you want to avoid writing the wrapper code and need a solution that can store objects and their attributes remotely (and having them completely hidden from the developers). 对于您的情况,如果我正确地阅读您,您希望避免编写包装器代码并需要一个可以远程存储对象及其属性的解决方案(并将它们完全隐藏在开发人员之外)。 I can't really suggest a better solution.. Umm, let me know if either of these ever come close to meet your requirements: 我真的不能建议一个更好的解决方案..嗯,如果其中任何一个接近满足您的要求,请告诉我:

  1. EJB3 / JPA EJB3 / JPA
  2. CouchDB (REST/JSON) CouchDB (REST / JSON)

Let me know too if I've interpreted your question wrongly. 如果我错误地解释了你的问题,请告诉我。

If we compare XML-RPC and SOAP, the latter will give you better data types handling, for the former although you will be dealing with simpler data types but you will have to write a layer or adapter to convert them to your domain objects. 如果我们比较XML-RPC和SOAP,后者将为您提供更好的数据类型处理,对于前者,尽管您将处理更简单的数据类型,但您必须编写一个层或适配器将它们转换为您的域对象。

yc YC

Soap is just a set of agreed upon XML schemas blessed by a standards group. Soap只是由标准组祝福的一组商定的XML模式。 It's strength is that it was designed for interoperability and it supports many enterprise-class features. 它的优势在于它是为互操作性而设计的,它支持许多企业级功能。 Soap on any platform will not provide the operations you are looking for. 任何平台上的肥皂都不会提供您正在寻找的操作。 You need to design & implement the service contract to get those features. 您需要设计并实施服务合同才能获得这些功能。

Sounds like you want remote objects which neither Soap or REST are particularly good for. 听起来你想要Soap或REST特别适合的远程对象。 Maybe you'd be better off looking at XML-RPC . 也许你最好不要看XML-RPC

The key differences are basically tooling. 关键差异基本上是工具。

Many of the high end SOAP stacks shroud the vast bulk of the SOAP infrastructure from the developer, to where you are working pretty much solely with DTO's/Documents and RPC. 许多高端SOAP堆栈将大量的SOAP基础架构从开发人员转移到几乎完全使用DTO / Documents和RPC的地方。

REST over HTTP puts more of that burden upon you the developer (negotiating formats [XML, JSON, HTTP POST], parsing results [DOM, maps, DTO marshalling, etc.]). REST over HTTP会给开发人员带来更多的负担(协商格式[XML,JSON,HTTP POST],解析结果[DOM,地图,DTO编组等])。

Obviously, you can write a layer of logic to make dealing with this detail easier. 显然,您可以编写一层逻辑,以便更轻松地处理此细节。 And some REST frameworks have arrived to make this easier, but at the moment, it's still a task on your TODO list when you wish to use or consume such services. 并且一些REST框架已经到位,以使这更容易,但目前,当您希望使用或使用此类服务​​时,它仍然是您的TODO列表上的任务。

My feedback is that if you want remote state, you are not talking about web services. 我的反馈是,如果你想要远程状态,你不是在谈论Web服务。 I don't know about any contemporary model that deal with remote state. 我不知道任何处理远程状态的现代模型。 I think that if you need remote state you are on your own ( without religion to follow ). 我认为,如果你需要远程状态,你就是自己(没有宗教信仰)。 Just throw xml from here to there and don't mind the theory. 把xml从这里扔到那里然后不介意理论。

I have to add that remote state is evil. 我必须补充说远程状态是邪恶的。 Avoid it if you can. 如果可以,请避免使用它。

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

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