Jaxb2Marshaller使用空名称空间URI创建JAXBContext [英] Jaxb2Marshaller creating JAXBContext with empty namespace URI

查看:294
本文介绍了Jaxb2Marshaller使用空名称空间URI创建JAXBContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Spring 3,我使用以下编组器创建了一个MarshallingView:

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>

MyNamespacePrefixMapper应该将Products对象的架构(生成的XJC)映射到默认命名空间,但它并不是因为Jaxb2Marshaller正在创建一个包含两个不同命名空间URI的JAXBContext。一个是我的架构,另一个是空字符串。空字符串会覆盖我分配默认命名空间的任何尝试。

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?

推荐答案

您可以尝试使用 MOXy JAXB 。 Spring配置保持不变,您只需使用以下条目在模型类中添加jaxb.properties文件:

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

参见 JAXB编组问题 - 可能与命名空间相关。您可以在标准@XmlSchema注释上配置namesapce前缀,而不是使用NamespacePrefixMapper:

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; 

这会产生如下XML:

<?xml version="1.0" encoding="UTF-8"?>
<process xmlns="http://www.example.org" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>

这篇关于Jaxb2Marshaller使用空名称空间URI创建JAXBContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆