如何使用cxf将命名空间添加到指定的元素?

时间:2015-04-15 13:06:16

标签: java xml cxf

我遇到cxf 3.0.1的问题。当我向服务发送请求时,cxf从我的元素的@XmlRootElement注释中获取命名空间并将其移动到正文的根元素。 例如:

A类:

@XmlSeeAlso(value=B.class)
public class A{

    @XmlAnyElement(lax=true)
    protected Object any;
    //gettres and setters
}

B组:

@XmlRootElement(name="B", namespace="http://elementBNamespace/")
public class B{
    //fields, getters and setters
}

当我发送请求时,它看起来像这样:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
    ...
</soap:Header>
<soap:Body xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="id-46f08307-9931-4494-a0a0-a87ae051d988">
    <ns3:rootElement
            xmlns:ns3="http://rootelementnamespace/"
            xmlns:ns4="http://namespace4/"
            xmlns:ns6="http://namespace6/"
            xmlns:ns7="http://elementBNamespace/">

     ...
        <ns4:A>
            <ns7:B>...<ns7:B>
        </ns4:A>
    </ns3:rootElement>
</soap:Envelope>

我需要的是:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
    ...
</soap:Header>
<soap:Body xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="id-46f08307-9931-4494-a0a0-a87ae051d988">
    <ns3:rootElement
            xmlns:ns3="http://rootelementnamespace/"
            xmlns:ns4="http://namespace4/"
            xmlns:ns6="http://namespace6/">

     ...
        <ns4:A>
            <ns7:B xmlns:ns7="http://elementBNamespace/">...<ns7:B>
        </ns4:A>
    </ns3:rootElement>
</soap:Envelope>

我该如何避免这种行为? 谢谢!

1 个答案:

答案 0 :(得分:1)

查看cxf转换功能http://cxf.apache.org/docs/transformationfeature.html。我认为部分&#34;更改输入和输出元素名称和命名空间&#34;从这个网站将帮助你。