将xml名称空间声明移动到带有jax-ws批注的根元素中

问题描述 投票:1回答:1

我正在尝试使用jax-ws生成xml有效负载,但无法正常工作。服务器希望所有名称空间都在信封标记中。例如:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://somewhere.namespace1.com" xmlns:ns2="http://somewhere.namespace2.com">

这是我需要的时间

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">

是我所拥有的。

jax-ws生成类似]的有效负载>

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Header/>
    <SOAP-ENV:Body>
        <ns1:Element1 xmlns:ns1="http://somewhere.namespace1.com" xmlns:ns1="http://somewhere.namespace2.com">
            <ns2:Element2>value</ns2:Element2>
        </ns1:Element1>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

但我需要

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://somewhere.namespace1.com" xmlns:ns2="http://somewhere.namespace2.com">
    <SOAP-ENV:Header/>
    <SOAP-ENV:Body>
        <ns1:Element1>
            <ns2:Element2>value</ns2:Element2>
        </ns1:Element1>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我已经尝试将package-info.java文件与@javax.xml.bind.annotation.XmlSchema放在一起,但是我可以更改前缀,但不能将实际的名称空间声明从子节点移动到根节点。例如,我可以(显然吗?)使用

定义信封中所需的所有名称空间。
@javax.xml.bind.annotation.XmlSchema(
    elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED,
    xmlns = {
            @XmlNs(
                prefix = "ns1",
                namespaceURI="http://somewhere.namespace1.com"),
            @XmlNs(
                prefix = "ns2",
                namespaceURI="http://somewhere.namespace2.com"),
    }
)

但是在package-info.javaElement1.java所在的Element2.java中,我不想在那里定义名称空间。我已经尝试过

@javax.xml.bind.annotation.XmlSchema(
    namespace="http://schemas.xmlsoap.org/soap/envelope/",
    location = ""
)

但是它不起作用。

还有其他人有类似的问题吗?我敢肯定这只是注解的问题,但我一直无法弄清楚。

我正在尝试使用jax-ws生成xml有效负载,但无法正常工作。服务器希望所有名称空间都在信封标记中。例如:

java xml soap jax-ws xml-namespaces
1个回答
0
投票

我通过使用Spring的WebServiceTemplate#sendAndReceive(String, WebServiceMessageCallback, WebServiceMessageExtractor<T>)进行调用解决了这个问题,其中在第二个参数(回调)中,我手动添加了需要在标头中使用的名称空间。例如类似

wsResponse = this.webServiceTemplate.sendAndReceive(uri,
        (webServiceMessage) -> {
            ...
            SoapMessage soapMessage = (SoapMessage) webServiceMessage;
            ...
            final SoapEnvelope envelope = soapMessage.getEnvelope();
            headerNamespaces.forEach(envelope::addNamespaceDeclaration);
            ...
        }, this.responseExtractor);
© www.soinside.com 2019 - 2024. All rights reserved.