开发者

Jaxb2Marshaller creating JAXBContext with empty namespace URI

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?


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 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"/>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜