【问题标题】:move xml namespace declarations to root element with jax-ws annotations使用 jax-ws 注释将 xml 命名空间声明移动到根元素
【发布时间】:2019-08-06 11:22:47
【问题描述】:

我正在尝试使用 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"),
    }
)

但是在Element1.javaElement2.java 所在的package-info.java 中,我不想在那里定义命名空间。我试过了

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

但它不起作用。

有没有其他人遇到过类似的问题?我确定这只是注释的问题,但我一直无法弄清楚。

【问题讨论】:

  • 我假设您已经使用“wsimport”来生成必要的客户端文件,这些文件将生成接口和实现文件。您还可以共享用于生成请求的 Java Web 服务客户端的代码吗?

标签: java xml soap jax-ws xml-namespaces


【解决方案1】:

我通过使用 Spring 的 WebServiceTemplate#sendAndReceive(String, WebServiceMessageCallback, WebServiceMessageExtractor&lt;T&gt;) 进行调用解决了这个问题,在第二个参数(回调)中,我手动添加了我需要在标题中的命名空间。例如像

wsResponse = this.webServiceTemplate.sendAndReceive(uri,
        (webServiceMessage) -> {
            ...
            SoapMessage soapMessage = (SoapMessage) webServiceMessage;
            ...
            final SoapEnvelope envelope = soapMessage.getEnvelope();
            headerNamespaces.forEach(envelope::addNamespaceDeclaration);
            ...
        }, this.responseExtractor);

【讨论】:

    猜你喜欢
    • 2013-07-10
    • 1970-01-01
    • 1970-01-01
    • 2014-01-27
    • 1970-01-01
    • 2014-04-24
    • 2016-10-30
    • 2011-05-06
    • 2012-08-16
    相关资源
    最近更新 更多