Marshaller and Unmarshaller - Java-Springs

As stated in the introduction, a marshaller serializes an object to XML, and an un marshaller deserializes XML stream to an object.

Marshaller

Spring abstracts all marshalling operations behind the org.spring frame work. 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.

Table:-

XMLoutput-abstraction_

Result implementation Wraps XML representation

DOM Result org.w3c.dom.Node
SAX Result org.xml.sax.Content Handler
Stream Result java.io.File, java.io. Out put Stream,
or java.io.Writer

Unmarshaller

Similar to the Marshaller, there is the org. spring frame work. oxm. Un marshaller 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.

Table:-

wraps-a-different-XML-representation,

Source implementation Wraps XML representation

DOM Source org.w3c.dom.Node
SAX Source org.xml.sax.InputSource, and
org.xml.sax.XMLReader
Stream Source java.io.File, java.io.In put Stream, or
java.io.Reader

Even though there are two separate marshalling interfaces (Marshaller and Un marshaller), 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.

Xml Mapping Exception

Spring converts exceptions from the underlying O/X mapping tool to its own exception hierarchy with the Xml Mapping Exception as the root exception. As can be expected, these runtime exceptions wrap the original exception so no information will be lost.

Additionally, the Marshalling Failure Exception and Un marshalling Failure Exception provide a distinction between marshalling and un marshalling operations, even though the under lying O/X mapping tool does not do so.

The O/X Mapping exception hierarchy is shown in the following figure:-

O/X Mapping exception hierarchy


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Java-Springs Topics