java 如何使用 Apache Axis2 和 WSDL2Java 向 SOAP 响应添加命名空间引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/74960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I add a namespace reference to a SOAP response with Apache Axis2 and WSDL2Java
提问by user13224
I'm looking at the SOAP output from a web service I'm developing, and I noticed something curious:
我正在查看我正在开发的 Web 服务的 SOAP 输出,我注意到一些奇怪的事情:
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
<soapenv:Body>
<ns1:CreateEntityTypesResponse xmlns:ns1="http://somedomain.com/wsinterface">
<newKeys>
<value>1234</value>
</newKeys>
<newKeys>
<value>2345</value>
</newKeys>
<newKeys>
<value>3456</value>
</newKeys>
<newKeys xsi:nil="1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<newKeys xsi:nil="1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<errors>Error1</errors>
<errors>Error2</errors>
</ns1:CreateEntityTypesResponse>
</soapenv:Body>
</soapenv:Envelope>
I have two newKeys elements that are nil, and both elements insert a namespace reference for xsi. I'd like to include that namespace in the soapenv:Envelope element so that the namespace reference is only sent once.
我有两个 nil 的 newKeys 元素,并且这两个元素都为 xsi 插入了一个命名空间引用。我想在 soapenv:Envelope 元素中包含该命名空间,以便命名空间引用只发送一次。
I am using WSDL2Java to generate the service skeleton, so I don't directly have access to the Axis2 API.
我使用 WSDL2Java 来生成服务框架,所以我不能直接访问 Axis2 API。
回答by Michael Sharek
Using WSDL2Java
使用 WSDL2Java
If you have used the Axis2 WSDL2Java tool you're kind of stuck with what it generates for you. However you can try to change the skeleton in this section:
如果您使用过 Axis2 WSDL2Java 工具,那么您就会对它为您生成的内容感到困惑。但是,您可以尝试更改本节中的骨架:
// create SOAP envelope with that payload
org.apache.axiom.soap.SOAPEnvelope env = null;
env = toEnvelope(
getFactory(_operationClient.getOptions().getSoapVersionURI()),
methodName,
optimizeContent(new javax.xml.namespace.QName
("http://tempuri.org/","methodName")));
//adding SOAP soap_headers
_serviceClient.addHeadersToEnvelope(env);
To add the namespace to the envelope add these lines somewhere in there:
要将命名空间添加到信封中,请在其中某处添加这些行:
OMNamespace xsi = getFactory(_operationClient.getOptions().getSoapVersionURI()).
createOMNamespace("http://www.w3.org/2001/XMLSchema-instance", "xsi");
env.declareNamespace(xsi);
Hand-coded
手工编码
If you are "hand-coding" the service you might do something like this:
如果您“手动编码”服务,您可能会执行以下操作:
SOAPFactory fac = OMAbstractFactory.getSOAP11Factory();
SOAPEnvelope envelope = fac.getDefaultEnvelope();
OMNamespace xsi = fac.createOMNamespace("http://www.w3.org/2001/XMLSchema-instance", "xsi");
envelope.declareNamespace(xsi);
OMNamespace methodNs = fac.createOMNamespace("http://somedomain.com/wsinterface", "ns1");
OMElement method = fac.createOMElement("CreateEntityTypesResponse", methodNs);
//add the newkeys and errors as OMElements here...
Exposing service in aar
在 aar 中暴露服务
If you are creating a service inside an aar you may be able to influence the SOAP message produced by using the target namespace or schema namespace properties (see this article).
如果您在 aar 内创建服务,您可能能够影响使用目标名称空间或模式名称空间属性生成的 SOAP 消息(请参阅本文)。
Hope that helps.
希望有帮助。
回答by dodoconr
Other option is that the variable MY_QNAME has the prefix empty.
另一个选项是变量 MY_QNAME 的前缀为空。
public static final QName MY_QNAME = new QName("http://www.hello.com/Service/",
"tagname",
"prefix");
So, if you fill it, then it works.
所以,如果你填满它,那么它就起作用了。

