tags:

views:

177

answers:

1

Using Spring 3, I have created a MarshallingView, with the following marshaller:

<bean name="xmlMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshalle r">
    <property name="classesToBeBound">
        <list>
            <value>com.mydomain.xml.schema.Products</value>
        </list>
    </property>
    <property name="marshallerProperties">
        <map>
            <entry key="com.sun.xml.bind.namespacePrefixMapper">
                <bean class="com.mydomain.xml.MyNamespacePrefixMapper"/>
            </entry>
        </map>
    </property>
</bean>

The MyNamespacePrefixMapper is supposed to map the schema of the Products object (XJC generated) to the default namespace, but it doesn't because the Jaxb2Marshaller is creating a JAXBContext that contains two different namespace URIs. One is my schema, the other one is a blank string. The blank string overrides any attempt by me to assign a default namespace.

Anyone know why this blank string is there or how I can get rid of it?

+1  A: 

You could try using MOXy JAXB. The Spring configuration remains the same, you simply need to add a jaxb.properties file in with your model classes with the following entry:

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

See http://stackoverflow.com/questions/3394644/jaxb-marshalling-problem-probably-namespace-related . Instead of using a NamespacePrefixMapper you can simply configure the namesapce prefixes on the standard @XmlSchema annotation:

@javax.xml.bind.annotation.XmlSchema( 
    namespace = "http://www.example.org", 
    xmlns = {
        @javax.xml.bind.annotation.XmlNs(prefix = "xsd", namespaceURI = "http://www.w3.org/2001/XMLSchema"),
    },
    elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED) 
package example; 

This produces XML like:

<?xml version="1.0" encoding="UTF-8"?>
<process xmlns="http://www.example.org" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/&gt;
Blaise Doughan
Unfortunately my objects are XJC generated ones so I can't manually edit the annotations. I'm not sure I can put a properties file in the package with them either, or can that be anywhere on the classpath?
Rob