Java Code Examples for org.springframework.oxm.jaxb.Jaxb2Marshaller#setMarshallerProperties()

The following examples show how to use org.springframework.oxm.jaxb.Jaxb2Marshaller#setMarshallerProperties() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExampleTest.java    From eclair with Apache License 2.0 6 votes vote down vote up
@Bean
public Jaxb2Marshaller jaxb2Marshaller() {
    Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
    marshaller.setClassesToBeBound(Dto.class);
    marshaller.setMarshallerProperties(singletonMap(Marshaller.JAXB_FRAGMENT, true));
    return marshaller;
}
 
Example 2
Source File: RestSpringModuleConfig.java    From herd with Apache License 2.0 6 votes vote down vote up
/**
 * Gets a new JAXB marshaller that is aware of our XSD and can perform schema validation. It is also aware of all our auto-generated classes that are in the
 * org.finra.herd.model.api.xml package. Note that REST endpoints that use Java objects which are not in this package will not use this marshaller and will
 * not get schema validated which is good since they don't have an XSD.
 *
 * @return the newly created JAXB marshaller.
 */
@Bean
public Jaxb2Marshaller jaxb2Marshaller()
{
    try
    {
        // Create the marshaller that is aware of our Java XSD and it's auto-generated classes.
        Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
        marshaller.setPackagesToScan("org.finra.herd.model.api.xml");
        marshaller.setSchemas(resourceResolver.getResources("classpath:herd.xsd"));

        // Get the JAXB XML headers from the environment.
        String xmlHeaders = configurationHelper.getProperty(ConfigurationValue.JAXB_XML_HEADERS);

        // We need to set marshaller properties to reconfigure the XML header.
        Map<String, Object> marshallerProperties = new HashMap<>();
        marshaller.setMarshallerProperties(marshallerProperties);

        // Remove the header that JAXB will generate.
        marshallerProperties.put(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);

        // Specify the new XML headers.
        marshallerProperties.put(ConfigurationValue.JAXB_XML_HEADERS.getKey(), xmlHeaders);

        // Specify a custom character escape handler to escape XML 1.1 restricted characters.
        marshallerProperties.put(MarshallerProperties.CHARACTER_ESCAPE_HANDLER, herdCharacterEscapeHandler);

        // Return the marshaller.
        return marshaller;
    }
    catch (Exception ex)
    {
        // Throw a runtime exception instead of a checked IOException since the XSD file should be contained within our application.
        throw new IllegalArgumentException("Unable to create marshaller.", ex);
    }
}