Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Enable REST with Web services, Part 1: REST and Web services in WSDL 2.0

Eran Chinthaka (eran.chinthaka@gmail.com), PMC Member, Apache Web services project
Eran Chinthaka is a pioneering member of the Apache Axis2, Axiom, and Synapse projects, and he is a member of WSDL 2.0 and WS-Addressing working groups.

Summary:  For clients to interact with remotely hosted resources, REpresentational State Transfer (REST) is fast becoming an alternative for Web services, especially because REST doesn't require users to understand and use SOAP. There are ongoing debates as to which one is better suited in today's highly interactive environment. However, recent efforts, including Web Services Description Language (WSDL) 2.0, have tried to give Web services the ability to benefit from REST and use REST concepts. The HTTP binding specification, available in WSDL 2.0 adjuncts, talks a lot about this. The first part of this article focuses on how REST is married to Web services in WSDL 2.0. The second part explains how it's being implemented in the Apache Web services project.

View more content in this series

Date:  24 May 2007
Level:  Intermediate
Also available in:   Japanese

Activity:  31823 views
Comments:  

What is REST?

In his dissertation, Architectural Styles and the Design of Network-based Software Architectures (see the Resources section for a link), Roy T. Fielding explains REST as this:

"Representational State Transfer is intended to evoke an image of how a well-designed Web application behaves: a network of web pages (a virtual state-machine), where the user progresses through an application by selecting links (state transitions), resulting in the next page (representing the next state of the application) being transferred to the user and rendered for their use."

So, in simple terms, REST is an architectural style for network systems. But let's try to quickly understand what REST is about.

In all of your Web searching, all you're really doing is locating a variety of artifacts, or resources. When you type http://ws.apache.org/axis2 into a browser to navigate the Apache Axis2 Web site, you get an HTML resource on a server. In other words, you get a representation of a resource located on that server. This resource representation puts you, accessing the resource, in a state. When you click a link on that page, you get a representation of another resource. Now the state of the client (you) is changed; in other words, you undergo a state transfer. When you continue to access various resources by following the links, you keep changing state. This is what REST means in simple terms.

It's important to note that REST by itself cannot be considered a standard.

What is SOAP?

Having understood what REST means, let's try to quickly recap what SOAP is. SOAP (which is considered to be not so simple anymore!) is a messaging protocol defined on top of XML. Two messaging entities can exchange data as SOAP messages, which both entities can understand due to its language independence. Take a look at a typical SOAP message.


Listing 1. Sample SOAP Message
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/03/addressing">
    <soapenv:Header>
        <wsa:MessageID>
            uuid:920C5190-0B8F-11D9-8CED-F22EDEEBF7E5</wsa:MessageID>
        <wsa:To>
            http://localhost:8081/axis/services/BankPort</wsa:To>
    </soapenv:Header>
    <soapenv:Body>
        <axis2:echo xmlns:axis2="http://ws.apache.org/axis2">Hello World </axis2:echo>
   </soapenv:Body>
</soapenv:Envelope>

SOAP messages contain an Envelope element, which encapsulates a mandatory Body element and the optional Header element. The Body element contains all the information that is to be delivered to the application. The Header is mostly used to provide quality of service (QoS) like WS-Addressing, WS-Security, WS-ReliableMessaging, and other related standards.

REST vs. Web services

Now look at some of the major differences between SOAP and REST:

  • REST is resource oriented, while Web services (WS) are service oriented..
  • REST mostly uses HTTP as the transport, while SOAP has no restriction in any means on a particular transport. Many people use REST style interactions using HTTP.
  • When you use REST, all the QoS parameters must be provided by the transport itself. In WS, there are lots of specifications built to support QoS options, using WS-* specifications.

Both models have their strong and weak points, and sometimes they become complementary. Because REST continuously promotes simplification, it has become sort of a burden on people who define new WS-* specifications to rethink the simplification.

How WSDL 2.0 enabled the marriage of REST and Web services

The WSDL 2.0 HTTP binding specification provides all the rules for this marriage. But it's worth noting that even though different people have different perspectives on what REST means, the objective in putting these two together is to support plain old XML (POX) interactions with services over basic HTTP operations like GET, POST, PUT, and DELETE.

Also it's important to keep in mind that we don't have an objective to completely support all of REST. This article covers how services that are exposed as Web services can also be exposed and accessed with REST-style interactions. It defines how to take (certain styles of) an XML Schema and formulate a GET/POST request out of it.

WSDL 2.0 rules for HTTP binding

Not all Web services can be exposed with all the various HTTP methods, and these restrictions are defined in the HTTP binding specification.

First, explore how to figure out whether an HTTP binding is available for a service. According to the WSDL 2.0 HTTP binding specification, the HTTP binding can be defined by setting the value of a type attribute of the <Binding> to http://www.w3.org/2006/01/wsdl/http.


Listing 2. WSDL binding element with HTTP binding
<description>
  <binding name="xs:NCName" interface="xs:QName"?
           type="http://www.w3.org/2006/01/wsdl/http"
           .....................
           ..................... />

When you create messages to be exchanged, you need to concentrate on the different serialization mechanisms. Before you can do that, you need to understand the various content types that are available with HTTP methods. For now, let's focus on the POST and GET methods.


Table 1. Content types supported with HTTP methods
HTTP MethodSupported Content Types
GETAll the parameters come in the request Uniform Resource Identifier (URI). The schema of the message must adhere to Internationalized Resource Identifier (IRI) style
POST
  • All or some of the parameters can come in the request URI. The schema of the message must adhere to IRI style
  • application/x-www-form-urlencoded — Some of the params are coming in the request URI, and some are coming in the body of the message. The schema of the message must adhere to IRI style.
  • application/xml — XML payload goes in the body of the POST request. There's no restriction on the schema of the content.

In addition to this, most of the other content types are also supported with POST method.

Contents of the messages and the payloads are defined either in MessageReference or InterfaceFault. These elements always refer to an element in the schema. Depending on the content type, there are restrictions on the schema of the XML payload, as mentioned in the above table. Now let's try to understand what IRI style is.

Internationalized Resource Identifier style

IRI style contains some restrictions that are defined on the schema of the payload, if you try to communicate with a service using GET method. IRI style is defined in the adjuncts of the WSDL 2.0 specification. Take a look at these restrictions:

  • The content model of the document element should be defined using a complex type that contains a sequence from XML Schema.
  • The sequence must contain only elements. It must not contain other structures, such as xs:choice.
  • The sequence must contain only local element children. These child elements may contain the nillable attribute.
  • The localPart of the document element's QName must be the same as the operations name.
  • The complex type that defines the body of the element or its children elements must not contain any attributes.
  • If the children elements of the sequence are defined using an XML Schema type, they must derive from xs:simpleType and must not be of the type or derive from xs:QName, xs:NOTATION, xs:hexBinary. or xs:base64Binary.

The IRI style ensures that the parameters you send using your request URI can be converted back in to a valid XML without much trouble. It's easiest to look at an example to understand it.

Try to retrieve user information with only the user's name and age. You need to send the information in Listing 3 to the service.


Listing 3. Sample user information
<GetInformation>
 <Name>MyName </Name>
 <Age>22 </Age>
</GetInformation>

How can you send this to the service in the request URI itself if you're using the GET method to invoke?

The answer is: http://myserver/PersonInfoService/GetInformation?Name=MyName&Age=22.

IRI style restricts your schema to define an XML sample, like the one above, because you can't send complicated XML using the GET method. If the Person element had another subelement, Address, which in turn has City and Country elements, then there's no way you can serialize this to request parameters without defining complicated rules.

Defining HTTP binding in WSDL

Now you'll see how an HTTP binding can be defined for a Web service operation.


Listing 4. WSDL binding operation
<description>
 <binding whttp:methodDefault="xs:string"?
          whttp:queryParameterSeparatorDefault="xs:string"?>
   <operation ref="xs:QName" 
              whttp:location="xs:anyURI"?
              whttp:method="xs:string"? 
              whttp:inputSerialization="xs:string"? 
              whttp:outputSerialization="xs:string"? 
              whttp:faultSerialization="xs:string"?
              whttp:queryParameterSeparator="xs:string"?>
  </operation>
 </binding>
</description>

An HTTP binding extension is designed to minimize what needs to be explicitly declared for common cases. As you can see, this is achieved by defining default values. Take a look at what the above WSDL fragment tries to express:

  • Binding element— You can define the default HTTP method that should be used using whttp:methodDefault and the separator for query parameters using whttp:queryParameterSeparatorDefault.
  • Operation element— For each and every operation you have the option of defining following parameters.
    1. location— Location of this operation. Most of the time this is a relative URI that needs to be combined with the endpoint base URI (Defined in the Endpoint element of the WSDL 2.0 document) to form a full URI. You can define a template for the URI here. This will be combined with the endpoint base to form a full URI to send the request.
    2. method— The HTTP method that can be used to invoke this operation. Value should be one of GET, POST, PUT, or DELETE.
    3. inputSerialization— How data should be serialized in sending a request to the service. This is defined by providing the proper content type.
    4. outputSerialization— How the response will be serialized to be sent. This is defined by providing the proper content type.
    5. faultSerialization— If a fault occurs, this is the serialization mechanism for that.
    6. queryParameterSeparator— The separator for query parameters.

The following are the default input and output serialization values available for GET and POST operations.


Table 2. Default input and output serializations
HTTP methodDefault input serializationDefault output serialization
GETapplication/x-www-form-urlencodedapplication/xml
POSTapplication/xmlapplication/xml

Does WSDL 2.0 enable REST?

The motivation of WSDL 2.0 HTTP binding is that it allows services to have both SOAP and HTTP bindings. The service implementation deals with processing application data, often represented as an XML element, and the service doesn't know whether that data came inside a SOAP envelope, HTTP GET, or HTTP POST. WSDL 2.0 HTTP binding enables you to expose a service as a resource to be invoked using HTTP methods. At the same time, you need to understand that HTTP binding doesn't enable you to implement a full REST style system. This is often debated by a lot of people, and it all depends on how much you believe in what REST can deliver.

Look for Part 2 of this article series, which explores how the above concepts can be used with two open source Web service engines: Apache Axis2/Java and Apache Axis2/C.


Resources

Learn

Get products and technologies

  • Innovate your next development project with IBM trial software, available for download or on DVD.

Discuss

About the author

Eran Chinthaka

Eran Chinthaka is a pioneering member of the Apache Axis2, Axiom, and Synapse projects, and he is a member of WSDL 2.0 and WS-Addressing working groups.

Report abuse help

Report abuse

Thank you. This entry has been flagged for moderator attention.


Report abuse help

Report abuse

Report abuse submission failed. Please try again later.


developerWorks: Sign in


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

Choose your display name

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


Rate this article

Comments

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=SOA and Web services
ArticleID=226249
ArticleTitle=Enable REST with Web services, Part 1: REST and Web services in WSDL 2.0
publish-date=05242007
author1-email=eran.chinthaka@gmail.com
author1-email-cc=flanders@us.ibm.com

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

For articles in technology zones (such as Java technology, Linux, Open source, XML), Popular tags shows the top tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), Popular tags shows the top tags for just that product zone.

For articles in technology zones (such as Java technology, Linux, Open source, XML), My tags shows your tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), My tags shows your tags for just that product zone.

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Try IBM PureSystems. No charge.

Special offers