Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

SOAP Basics

This section reviews the basics of SOAP. After reading this section, you will be able to find your way through common SOAP interactions. The next section zooms in on some of these topics and provides the missing details.

At the time of writing, SOAP is not an official W3C standard. Two somewhat incompatible versions of SOAP coexist:

Note that SOAP 1.1 is not officially endorsed by W3C. It is available only on the W3C site as notes. Notes (as opposed to recommendations such as XML) are working documents prepared by W3C members and published for information only. Notes are intended to elicit comments.

The most visible difference between SOAP 1.1 and SOAP 1.2 is the use of XML namespaces. SOAP 1.1 builds all its namespaces from the http://schemas.xmlsoap.org/soap root, whereas SOAP 1.2 uses http://www.w3.org/2001/09/soap.


Because they use different namespaces, the two versions of SOAP are not directly compatible. Otherwise they are mostly similar.

The remainder of this article concentrates on the similarities and highlights the differences only where relevant.

Other differences are mostly editorial. SOAP 1.2 is better written: it contains fewer ambiguities and is generally easier to read.


If you have problems when reading the SOAP 1.1 specification, look up the corresponding section in the SOAP 1.2 spec.

SOAP Architecture

The most striking difference between SOAP and XML-RPC is that SOAP leaves so many options open. XML-RPC provided a closed solution to one problem: sending RPC requests over HTTP.

SOAP, on the other hand, is more like a toolbox. It defines a messaging framework (the envelope), encoding rules and a binding to the HTTP protocol. RPC needs all three but you can build SOAP requests with the envelope, the HTTP binding, and then use other encoding rules. Or you can use the envelope, the encoding rules, and another communication protocol.

Figure 1 illustrates this. At the bottom of the stack are HTTP and SMTP, two communication protocols developed independently from SOAP. They control sending and receiving files.

Figure 1 Logical view of SOAP.

SOAP attaches to those protocols through bindings (note that, at the time of writing, the SMTP binding is not official). On top of the binding comes the message framework that lays down the rules that organize SOAP messages. What goes in the messages? That's the role of the encoding rules. Finally the RPC uses the encoding rules to create a message, which is stored in an envelope (the message framework), and sent over HTTP.

Notice the ebXML Transport, Routing, and Packaging in Figure 1. The ebXML initiative develops e-commerce solutions with XML (see http://www.ebxml.org for more information). It uses the messaging framework and the SOAP bindings, but it has its own encoding rules. The ebXML initiative helps illustrate that SOAP is a toolbox from which you can pick and choose those elements that make sense for you.

Unfortunately, what is gained in flexibility is lost in simplicity. SOAP can be very confusing when you try to find your way through the maze of options. Also the options may result in incompatibilities. For example, although ebXML uses SOAP, it is not directly compatible with SOAP RPC because it does not use the same encoding rules.


If you ever get lost in the SOAP options, remember that, taken individually, each option is simple. Although the number of options may be confusing, they are not difficult to understand. If you understand how two options relate one to another, you should do fine.

SOAP Blocks and Elements

The messaging framework defines the basic XML elements you need to construct SOAP requests. SOAP defines the request in terms of SOAP blocks where a block is a single logical computational unit. The block is represented as an XML element identified by a qualified name, that is, a combination of local name and namespace.

XML Namespaces

A SOAP message contains elements defined by different sources: some are defined by the SOAP recommendation itself, others come from the XML schema recommendation and, finally, it is likely to also contain elements defined by the programmer.

The fact that different sources collaborate to create a document may cause conflicts and, in particular, naming conflicts.

XML namespaces is a standard mechanism (defined by the W3C) to avoid name conflicts. For example, if the programmer has defined a Body element, the namespace helps distinguish this Body element from the SOAP body. In that respect, namespaces are similar to Java packages.

To guarantee uniqueness, XML namespaces are URIs that are bound to prefixes through an xmlns attribute.

For example,

<env:Envelope xmlns:env=http://schemas.xmlsoap.org/soap/envelope/">

binds the http://schemas.xmlsoap.org/soap/envelope/ namespace with the env prefix. Only the prefix appears in the remainder of the document. For example:


The name after the prefix is said to be the local name. Again note the similarity with Java packages that also use domain names to guarantee the uniqueness of names.

Unfortunately, because SOAP uses HTTP, SOAP nodes are also identified by URIs, and this creates some confusion. To avoid confusion, remember that URIs to xmlsoap.org and w3.org are likely to be namespaces.

SOAP defines four XML elements:

  • env:Envelope is the root of the SOAP request. At the minimum, it defines the SOAP namespace (http://schemas.xmlsoap.org/soap/envelope/ for SOAP 1.1 and http://www.w3.org/2001/09/soap-envelope for SOAP 1.2; the latter URI is likely to change). It may define additional namespaces.

  • env:Header contains auxiliary information, as SOAP blocks, such as authentication, routing information, or transaction identifier. The header is optional.

  • env:Body contains the main information in one or more SOAP blocks. An example would be a SOAP block for RPC call. The body is mandatory and it must appear after the header.

  • env:Fault is a special block that indicates protocol-level errors. If present, it must appear in the body.


The SOAP 1.1 specification uses SOAP-ENV for the prefix throughout the specification. Consequently most SOAP 1.1 libraries use the SOAP-ENV prefix. This article uses env, as in the SOAP 1.2 recommendation. Bear in mind that the prefix is irrelevant as long as the URI remains correct.

Figure 2 illustrates the physical organization of a SOAP message. Notice that the header appears before the body.

Figure 2 Physical layout of a SOAP message.

SOAP requests may be sent directly to the server or they may be routed through one or more so-called SOAP nodes. A typical example is when a message is routed through proxies on its way to the final recipient, as Figure 3 illustrates.

Figure 3 Forwarding a message through a proxy.

Some or all the SOAP blocks in the headers may be targeted to intermediary SOAP nodes, as was shown in Figure 3. In this example, the header contains routing information that is intended for the proxy only. Notice that the proxy has processed and removed the information from the message it forwards to the ultimate recipient. Unlike the header, the body is always routed to the ultimate SOAP node unmodified.

Encoding Styles

With the exception of the env:Fault element, which signals protocol errors, SOAP leaves it up to you to decide what goes in the header and body. Still, SOAP defines a so-called encoding style to create XML documents from data structures. The SOAP encoding style defines XML equivalents for data structures commonly found in programming languages: simple types (integer, string, and the like), arrays, and structures.

For arrays and structures, SOAP defines an accessor, which is an identifier for the data. In an array, the accessor is the position of the element; in a structure, the accessor is the name field.

SOAP maps all the information to XML elements. For example, a name field will become a name element in the XML document. Instances of the Java class in Listing 7 are represented with the XML document in Listing 8 (using SOAP 1.2 namespaces).

Listing 7—Resource.java

package com.psol.resourceful;

public class Resource
  public int id = -1;
  public String name = null,
         description = null;

Listing 8—SOAP Encoding for a Resource

<?xml version="1.0"?>
<rs:Resource xmlns:rs="http://www.psol.com/2001/resourceful"
  <id xsi:type="xsd:int">0</id>
  <description>Our largest meeting room</description>

SOAP defines the env:encodingStyle attribute to indicate the rules used to create a document. The value of env:encodingStyle is a list of URIs that identify the rules to decode the document. The attribute may be attached to any element and its scope extends to the descendant of the element (not unlike XML namespaces).

The URIs for SOAP's own encoding rules are http://schemas.xmlsoap.org/soap/encoding/ for SOAP 1.1 and http://www.w3.org/2001/09/soap-envelope for SOAP 1.2. You can see the env:encodingStyle attribute on line 6 of Listing 8.

Transport Options

SOAP defines an HTTP binding. Essentially a SOAP message is sent using the POST verb. HTTP also supports GET, HEAD, and PUT, but SOAP does not use them.

SOAP with attachments is an extension to SOAP 1.1 that supports sending attachments next to the SOAP body. Attachments are similar to mail attachments and may include images or other files.

Although it only defines the binding for HTTP, the SOAP recommendation makes it clear that binding SOAP to other protocols is possible. In practice, there's great interest in binding SOAP over SMTP, the email protocol. Obviously, when bound over SMTP, SOAP provides one-way communication only: A message is sent but there is no response. If the recipient wants to reply, it needs to send another request.


Armed with encoding rules, an envelope, and HTTP binding, it's not difficult to support RPC. For SOAP, an RPC request is a structure where each in and in/out parameter is given its own accessor. For example, a call to the Java method getFreeResourcesOn(Date start,Date end) is encoded as the following structure:

struct GetFreeResourcesOn {
  TimeInstant start;
  TimeInstant end;

or, using the SOAP encoding rules introduced in the earlier "Encoding Styles" section (using SOAP 1.1 namespaces):

<?xml version='1.0'?>
<start xsi:type="xsd:timeInstant">2001-01-15T00:00:00Z</start>
<end xsi:type="xsd:timeInstant">2001-01-17T00:00:00Z</end>

The response is also a struct with one accessor for the return value and one accessor for each of the out and in/out parameters.


Java does not support in/out or out parameters. It supports only in parameters.

  • + Share This
  • 🔖 Save To Your Account

Related Resources