`

WebService: SOAP VS. RESTFul

阅读更多
Understanding SOAP and REST Basics And Differences
January 8, 2013 by John Mueller

一、背景知识


1、什么是 WebService ?

-
   Web 上的 Service。

   谁的 Service? 对象不是人。是机器。

   人通过Web获取的信息都是利于人阅读的。而 WebService 提供的信息是利于机器阅读的。

   所以 WebService 的全名是: Application Web Service 。面向应用的web服务。

-
   为什么需要 WebService?

   可以提供系统与系统间的通信。可以构建复杂的分布式系统。


   WebService 只是一个抽象的概念。只要能够提供面向应用的web服务,都可以成为 WebService。
   它可以用 Java 实现,可以用 .NET 实现。
  
   本文只针对 Java 实现。


二、SOAP VS. REST

Simple Object Access Protocol (SOAP) and Representational State Transfer (REST) are two answers to the same question: how to access Web services. The choice initially may seem easy, but at times it can be surprisingly difficult.
实现 WebService 的方式中, SOAP 和 REST 是其中两种。选择哪一种乍看起来简单,但是多数情况下,其实相当困难。


SOAP is a standards-based Web services access protocol that has been around for a while and enjoys all of the benefits of long-term use. Originally developed by Microsoft, SOAP really isn’t as simple as the acronym would suggest.
SOAP是基于规范的一种 WebService 实现协议。(注意:这里的协议不属于网络七层协议,而是用户用于数据传输而自定义的协议。SOAP 是所有自定义协议的统称。)SOAP已经使用了一段时间,带来了很大方便,并有初具规模的长期服务。SOAP 最初由 Microsoft 开发。SOAP 并不像它简称上写的那样简单。


__________________________________________________________________________________

关于 RESTful :
-
    Representational State Transfer
   
    which state ? The state of object.

-

    Representational State of Object Transfer
   
    对象的表示化状态-传输。 (表示成什么呢?文本)

    比较专业的翻译: 具象状态传输 (对象的状态具文本象,传输)

    把如此专业的概念说的如此抽象。不就是字符串么?不仅仅是。
    字符串只是对象的一种象。对象的象还可以是二进制字节流。
    但,RESTful 目前只支持字符串,貌似。
-  
   
_________________________________________________________________________________



三、The Difference between SOAP vs REST APIs

REST is the newcomer to the block. It seeks to fix the problems with SOAP and provide a truly simple method of accessing Web services. However, sometimes SOAP is actually easier to use; sometimes REST has problems of its own. Both techniques have issues to consider when deciding which protocol to use.
REST 是新来的。它寻求解决 SOAP 的不足和问题,提供了一种够简单的 WebService 的实现方案,然而,特定情况下 SOAP 使用起来更简单。 REST 也有自己的不足。当选择时,它们各自的问题和不足都要考虑到。

Before I go any further, it’s important to clarify that while both SOAP and REST share similarities over the HTTP protocol, SOAP is a more rigid set of messaging patterns than REST. The rules in SOAP are important because without these rules, you can’t achieve any level of standardization. REST as an architecture style does not require processing and is naturally more flexible. Both SOAP and REST rely on well-established rules that everyone has agreed to abide by in the interest of exchanging information.
在继续讨论之前,首先声明:SOAP 是基于协议的。它在交换数据时必须同时携带定义数据的协议数据。虽然 SOAP 和 REST 在使用 HTTP 协议上有相同点,但是 SOAP 是硬性要求传递数据解析的格式。如果这些格式缺失或被省略,SOAP 无法工作。而 REST 则是一种构建模式形式,不硬性要求信息的解析处理,所以原生更灵活。当然,两者都需要知道数据的格式才可以数据的交换。



A Quick Overview of SOAP
SOAP relies exclusively on XML to provide messaging services. Microsoft originally developed SOAP to take the place of older technologies that don’t work well on the Internet such as the Distributed Component Object Model (DCOM) and Common Object Request Broker Architecture (CORBA). These technologies fail because they rely on binary messaging; the XML messaging that SOAP employs works better over the Internet.

After an initial release, Microsoft submitted SOAP to the Internet Engineering Task Force (IETF) where it was standardized. SOAP is designed to support expansion, so it has all sorts of other acronyms and abbreviations associated with it, such as WS-Addressing, WS-Policy, WS-Security, WS-Federation, WS-ReliableMessaging, WS-Coordination, WS-AtomicTransaction, and WS-RemotePortlets. In fact, you can find a whole laundry list of these standards on Web Services Standards.

The point is that SOAP is highly extensible, but you only use the pieces you need for a particular task. For example, when using a public Web service that’s freely available to everyone, you really don’t have much need for WS-Security.

The XML used to make requests and receive responses in SOAP can become extremely complex. In some programming languages, you need to build those requests manually, which becomes problematic because SOAP is intolerant of errors. However, other languages can use shortcuts that SOAP provides; that can help you reduce the effort required to create the request and to parse the response. In fact, when working with .NET languages, you never even see the XML.

Part of the magic is the Web Services Description Language (WSDL). This is another file that’s associated with SOAP. It provides a definition of how the Web service works, so that when you create a reference to it, the IDE can completely automate the process. So, the difficulty of using SOAP depends to a large degree on the language you use.

One of the most important SOAP features is built-in error handling. If there’s a problem with your request, the response contains error information that you can use to fix the problem. Given that you might not own the Web service, this particular feature is extremely important; otherwise you would be left guessing as to why things didn’t work. The error reporting even provides standardized codes so that it’s possible to automate some error handling tasks in your code.

An interesting SOAP feature is that you don’t necessarily have to use it with the HyperText Transfer Protocol (HTTP) transport. There’s an actual specification for using SOAP over Simple Mail Transfer Protocol (SMTP) and there isn’t any reason you can’t use it over other transports. In fact, developers in some languages, such as Python and PHP, are doing just that.



A Quick Overview of REST
Many developers found SOAP cumbersome and hard to use. For example, working with SOAP in JavaScript means writing a ton of code to perform extremely simple tasks because you must create the required XML structure absolutely every time.

REST provides a lighter weight alternative. Instead of using XML to make a request, REST relies on a simple URL in many cases. In some situations you must provide additional information in special ways, but most Web services using REST rely exclusively on obtaining the needed information using the URL approach. REST can use four different HTTP 1.1 verbs (GET, POST, PUT, and DELETE) to perform tasks.

Unlike SOAP, REST doesn’t have to use XML to provide the response. You can find REST-based Web services that output the data in Command Separated Value (CSV), JavaScript Object Notation (JSON) and Really Simple Syndication (RSS). The point is that you can obtain the output you need in a form that’s easy to parse within the language you need for your application.

As an example of working with REST, you could create a URL for Weather Underground. The API’s documentation page shows an example URL of http://api.wunderground.com/api/Your_Key/conditions/q/CA/San_Francisco.json. The information you receive in return is a JSON formatted document containing the weather for San Francisco. You can use your browser to interact with the Web service, which makes it a lot easier to create the right URL and verify the output you need to parse with your application.



Deciding Between SOAP and REST
Before you spend hours fretting over the choice between SOAP and REST, consider that some Web services support one and some the other. Unless you plan to create your own Web service, the decision of which protocol to use may already be made for you. Extremely few Web services, such as Amazon, support both. The focus of your decision often centers on which Web service best meets your needs, rather than which protocol to use.



Soap Vs Rest
SOAP is definitely the heavyweight choice for Web service access. It provides the following advantages when compared to REST:

- Language, platform, and transport independent (REST requires use of HTTP)
- Works well in distributed enterprise environments (REST assumes direct point-to-point communication)
   SOAP 可以传递序列化的对象,二进制数据流。该特点在分布式环境中发挥的很好。
   REST 目前的支持的数据传递格式为字符文本。故只能是点对点传输。

- Standardized
- Provides significant pre-build extensibility in the form of the WS* standards
- Built-in error handling
- Automation when used with certain language products



REST is easier to use for the most part and is more flexible. It has the following advantages when compared to SOAP:

- No expensive tools require to interact with the Web service
- Smaller learning curve
- Efficient (SOAP uses XML for all messages, REST can use smaller message formats)
- Fast (no extensive processing required)
- Closer to other Web technologies in design philosophy








-
转载请注明,
原文出处:http://lixh1986.iteye.com/blog/2346064




-


Java Web Service 20 问





分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics