Chapter 4. Shared components


In this chapter, we will explore the the components which are shared between client- and server side Spring-WS development. These interfaces and classes represent the building blocks of Spring-WS, so it's important to understand what they do, even if you do not use them directly.

4.1. Web service messages

4.1.1. WebServiceMessage

One of the core interfaces within Spring Web Services is the WebServiceMessage. This interface represents a protocol agnostic XML message. The interface contains methods that provide access to the payload of the message, in the form of a javax.xml.transform.Source or a javax.xml.transform.Result. Source and Result are tagging interfaces that represent an abstraction over XML input and output. Concrete implementations wrap various XML representations, as indicated in the table below.

Source/Result implementationWraps XML representation
javax.xml.transform.dom.DOMSource org.w3c.dom.Node
javax.xml.transform.dom.DOMResult org.w3c.dom.Node
javax.xml.transform.sax.SAXSource org.xml.sax.InputSource and org.xml.sax.XMLReader
javax.xml.transform.sax.SAXResult org.xml.sax.ContentHandler
javax.xml.transform.stream.StreamSource java.io.File, java.io.InputStream, or java.io.Reader
javax.xml.transform.stream.StreamResult java.io.File, java.io.OutputStream, or java.io.Writer

In addition to reading from and writing to the payload, a Web service message can write itself to an output stream.

4.1.2. SoapMessage

The SoapMessage is an extension of WebServiceMessage. It contains SOAP-specific methods, such as getting SOAP Headers, SOAP Faults, etc. Generally, your code should only not be dependent on SoapMessage, because the content of the SOAP Body can be obtained via getPayloadSource() and getPayloadResult() in the WebServiceMessage. Only when it is necessary to perform SOAP-specific actions, such as adding a header, get an attachment, etc., should you need to cast WebServiceMessage to SoapMessage.

4.1.3. Message Factories

Concrete message implementation are created by a WebServiceMessageFactory. This factory can create an empty message, or read a message based on an input stream. There are two concrete implementations of WebServiceMessageFactory. One is based on SAAJ, the SOAP with Attachments API for Java, the other based on Axis 2's AXIOM, the AXis Object Model.

4.1.3.1. SaajSoapMessageFactory

The SaajSoapMessageFactory uses the SOAP with Attachments API for Java to create SoapMessage implementations. SAAJ is part of J2EE 1.4, so it should be supported under most modern application servers. You wire up a SaajSoapMessageFactory like so:

<bean id="messageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory" />

Note

SAAJ is based on DOM, the Document Object Model. This means that all SOAP messages are stored in memory as a whole. For larger SOAP messages, this may not be very performant. In that case, the AxiomSoapMessageFactory might be more applicable.

4.1.3.2. AxiomSoapMessageFactory

The AxiomSoapMessageFactory uses the AXis 2 Object Model to create SoapMessage implementations. AXIOM is based on StAX, the Streaming API for XML. StAX provides a pull-based mechanism for reading XML messages, which can be more efficient for larger messages.

To increase reading performance on the AxiomSoapMessageFactory, you can set the payloadCaching property to false (default is true). This this will read the contents of the SOAP body directly from the stream. When this setting is enabled, the payload can only be read once. This means that you have to make sure that any preprocessing of the message does not consume it.

You use the AxiomSoapMessageFactory as follows:

<bean id="messageFactory" class="org.springframework.ws.soap.axiom.AxiomSoapMessageFactory">
    <property name="payloadCaching" value="true"/>
</bean>