8

我正在查看我正在开发的 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>

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

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

4

2 回答 2

7

Using 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:

   // 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

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).

Hope that helps.

于 2008-09-16T18:00:28.863 回答
1

其他选项是变量 MY_QNAME 的前缀为空。

public static final QName MY_QNAME = new QName("http://www.hello.com/Service/",
            "tagname",
            "prefix");

因此,如果您填写它,那么它就可以工作。

于 2012-06-22T19:17:37.570 回答