CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-springframework--spring-oxm

Spring Object/XML Marshalling support providing generic interfaces for converting Java objects to XML and vice versa

Overview
Eval results
Files

core-marshalling.mddocs/

Core Marshalling Interfaces

The core marshalling interfaces provide the foundation for all XML marshalling and unmarshalling operations in Spring OXM. These interfaces define generic contracts that can be implemented by various XML processing technologies.

Core Interfaces

Marshaller

The primary interface for converting Java objects to XML.

public interface Marshaller {
    /**
     * Indicate whether this marshaller can marshal instances of the supplied type.
     * @param clazz the class that this marshaller is being asked if it can marshal
     * @return true if this marshaller can indeed marshal instances of the supplied class; false otherwise
     */
    boolean supports(Class<?> clazz);

    /**
     * Marshal the object graph with the given root into the provided Result.
     * @param graph the root of the object graph to marshal
     * @param result the result to marshal to
     * @throws IOException if an I/O error occurs
     * @throws XmlMappingException if the given object cannot be marshalled to the result
     */
    void marshal(Object graph, Result result) throws IOException, XmlMappingException;
}

Unmarshaller

The primary interface for converting XML to Java objects.

public interface Unmarshaller {
    /**
     * Indicate whether this unmarshaller can unmarshal instances of the supplied type.
     * @param clazz the class that this unmarshaller is being asked if it can marshal
     * @return true if this unmarshaller can indeed unmarshal to the supplied class; false otherwise
     */
    boolean supports(Class<?> clazz);

    /**
     * Unmarshal the given Source into an object graph.
     * @param source the source to marshal from
     * @return the object graph
     * @throws IOException if an I/O error occurs
     * @throws XmlMappingException if the given source cannot be mapped to an object
     */
    Object unmarshal(Source source) throws IOException, XmlMappingException;
}

Generic Type Support

Extended interfaces that provide support for generic types and parameterized types.

public interface GenericMarshaller extends Marshaller {
    /**
     * Indicates whether this marshaller can marshal instances of the supplied generic type.
     * @param genericType the type that this marshaller is being asked if it can marshal
     * @return true if this marshaller can indeed marshal instances of the supplied type; false otherwise
     */
    boolean supports(Type genericType);
}

public interface GenericUnmarshaller extends Unmarshaller {
    /**
     * Indicates whether this unmarshaller can unmarshal instances of the supplied generic type.
     * @param genericType the type that this unmarshaller is being asked if it can unmarshal
     * @return true if this unmarshaller can indeed unmarshal instances of the supplied type; false otherwise
     */
    boolean supports(Type genericType);
}

Usage Examples

Basic Marshalling

import org.springframework.oxm.Marshaller;
import javax.xml.transform.stream.StreamResult;
import java.io.StringWriter;

// Assuming marshaller is configured
Marshaller marshaller = // ... get marshaller instance

// Check if marshaller supports the type
if (marshaller.supports(MyClass.class)) {
    MyClass object = new MyClass();
    StringWriter writer = new StringWriter();
    
    // Marshal to XML
    marshaller.marshal(object, new StreamResult(writer));
    String xmlResult = writer.toString();
}

Basic Unmarshalling

import org.springframework.oxm.Unmarshaller;
import javax.xml.transform.stream.StreamSource;
import java.io.StringReader;

// Assuming unmarshaller is configured
Unmarshaller unmarshaller = // ... get unmarshaller instance

String xmlData = "<myClass>...</myClass>";

// Check if unmarshaller supports the type
if (unmarshaller.supports(MyClass.class)) {
    StringReader reader = new StringReader(xmlData);
    
    // Unmarshal from XML
    MyClass result = (MyClass) unmarshaller.unmarshal(new StreamSource(reader));
}

Generic Type Support

import org.springframework.oxm.GenericMarshaller;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

GenericMarshaller genericMarshaller = // ... get generic marshaller

// Create a parameterized type for List<MyClass>
ParameterizedType listType = new ParameterizedType() {
    public Type[] getActualTypeArguments() { return new Type[]{MyClass.class}; }
    public Type getRawType() { return List.class; }
    public Type getOwnerType() { return null; }
};

// Check generic type support
if (genericMarshaller.supports(listType)) {
    List<MyClass> objectList = Arrays.asList(new MyClass(), new MyClass());
    // Marshal the generic collection...
}

Required Imports

import org.springframework.oxm.Marshaller;
import org.springframework.oxm.Unmarshaller;
import org.springframework.oxm.GenericMarshaller;
import org.springframework.oxm.GenericUnmarshaller;
import org.springframework.oxm.XmlMappingException;

import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import java.io.IOException;
import java.lang.reflect.Type;

Error Handling

All marshalling operations can throw XmlMappingException or its subclasses. Common scenarios include:

  • MarshallingFailureException: When object cannot be converted to XML
  • UnmarshallingFailureException: When XML cannot be converted to object
  • ValidationFailureException: When validation fails during marshalling
  • UncategorizedMappingException: For other mapping-related errors

Always handle these exceptions appropriately in your application code.

Install with Tessl CLI

npx tessl i tessl/maven-org-springframework--spring-oxm

docs

core-marshalling.md

index.md

jaxb-implementation.md

mime-support.md

support-utilities.md

xstream-implementation.md

tile.json