As stated in the introduction, a marshaller serializes an object to XML, and an unmarshaller deserializes XML stream to an object. In this section, we will describe the two Spring interfaces used for this purpose.
Spring abstracts all marshalling operations behind the
org.springframework.oxm.Marshaller
interface, the main methods of which
is listed below.
public interface Marshaller { /** * Marshals the object graph with the given root into the provided Result. */ void marshal(Object graph, Result result) throws XmlMappingException, IOException; }
The Marshaller
interface has one main method, which marshals the given
object to a given javax.xml.transform.Result
. Result is a tagging
interface that basically represents an XML output abstraction: concrete implementations wrap various XML
representations, as indicated in the table below.
Result implementation | Wraps XML representation |
---|---|
DOMResult | org.w3c.dom.Node |
SAXResult | org.xml.sax.ContentHandler |
StreamResult |
java.io.File ,
java.io.OutputStream , or
java.io.Writer
|
Note | |
---|---|
Although the |
Similar to the Marshaller
, there is the
org.springframework.oxm.Unmarshaller
interface.
public interface Unmarshaller { /** * Unmarshals the given provided Source into an object graph. */ Object unmarshal(Source source) throws XmlMappingException, IOException; }
This interface also has one method, which reads from the given
javax.xml.transform.Source
(an XML input abstraction), and returns the
object read. As with Result, Source is a tagging interface that has three concrete implementations. Each
wraps a different XML representation, as indicated in the table below.
Source implementation | Wraps XML representation |
---|---|
DOMSource | org.w3c.dom.Node |
SAXSource |
org.xml.sax.InputSource , and
org.xml.sax.XMLReader
|
StreamSource |
java.io.File ,
java.io.InputStream , or
java.io.Reader
|
Even though there are two separate marshalling interfaces (Marshaller
and Unmarshaller
), all implementations found in Spring-WS implement both in
one class. This means that you can wire up one marshaller class and refer to it both as a marshaller and an
unmarshaller in your applicationContext.xml
.
Spring converts exceptions from the underlying O/X mapping tool to its own exception hierarchy with the
XmlMappingException
as the root exception. As can be expected, these runtime
exceptions wrap the original exception so no information will be lost.
Additionally, the MarshallingFailureException
and
UnmarshallingFailureException
provide a distinction between marshalling and
unmarshalling operations, even though the underlying O/X mapping tool does not do so.
The O/X Mapping exception hierarchy is shown in the following figure: