[英]move xml namespace declarations to root element with jax-ws annotations
我正在嘗試使用 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
一起package-info.java
,並且我能夠更改前綴但不能將實際的命名空間聲明從子節點移動到根節點。 例如,我可以(顯然?)在信封中定義我需要的所有命名空間
@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.java
和Element2.java
所在的package-info.java
中,我不希望在那里定義命名空間。 我試過了
@javax.xml.bind.annotation.XmlSchema(
namespace="http://schemas.xmlsoap.org/soap/envelope/",
location = ""
)
但它不起作用。
有沒有其他人遇到過類似的問題? 我確定這只是注釋的問題,但我一直無法弄清楚。
我通過調用 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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.