如何使用Apache Axis2和WSDL2Java向SOAP响应添加命名空间引用

use*_*224 8 java soap axis2 namespaces wsdl2java

我正在查看我正在开发的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>
Run Code Online (Sandbox Code Playgroud)

我有两个newKeys元素是nil,两个元素都插入了xsi的命名空间引用.我想在soapenv:Envelope元素中包含该命名空间,以便命名空间引用只发送一次.

我使用WSDL2Java生成服务框架,因此我无法直接访问Axis2 API.

Mic*_*rek 7

使用WSDL2Java

如果您已经使用了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);
Run Code Online (Sandbox Code Playgroud)

要将命名空间添加到信封中,请在其中的某处添加以下行:

OMNamespace xsi = getFactory(_operationClient.getOptions().getSoapVersionURI()).
    createOMNamespace("http://www.w3.org/2001/XMLSchema-instance", "xsi");

env.declareNamespace(xsi);
Run Code Online (Sandbox Code Playgroud)

手工编码

如果您对该服务进行"手动编码",您可能会执行以下操作:

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...
Run Code Online (Sandbox Code Playgroud)

在aar公开服务

如果要在aar中创建服务,则可能会影响使用目标命名空间或模式命名空间属性生成的SOAP消息(请参阅此文章).

希望有所帮助.