【问题标题】:how to edit the response fields in node-soap如何在 node-soap 中编辑响应字段
【发布时间】:2021-12-22 19:00:29
【问题描述】:

我有以下 WSDL 定义:

<definitions targetNamespace="http://app.com/app" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:app="http://app.com/app" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   
   <message name="SoapQuery">
      <part name="TransType" type="xsd:string" />
   </message>

   <message name="SoapQueryResult">
      <part name="ResponseCode" type="xsd:string"/>
      <part name="ResultDesc" type="xsd:string" />
   </message>

   <portType name="SoapQuery_PortType">
      <operation name="SoapQuery">
         <input message="SoapQuery" />
         <output message="SoapQueryResult" />
      </operation>
   </portType>

   <binding name="SoapQuery_Binding" type="SoapQuery_PortType">
      <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />
      <operation name="SoapQuery" style="document">
         <soap:operation soapAction="SoapQuery" />
         <soap:input>
            <soap:body namespace="app" use="literal" />
         </soap:input>

         <soap:output>
            <soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" use="literal" />
         </soap:output>
      </operation>
   </binding>

   <service name="SoapQueryService">
      <documentation>WSDL File for SoapQueryService</documentation>
      <port binding="SoapQuery_Binding" name="SoapQuery_Port">
         <soap:address location="http://localhost:8002/api/request" />
      </port>
   </service>
</definitions>

以及以下处理程序定义:

var SoapQueryService = {
  SoapQueryService: {
    SoapQuery_Port: {
      // This is how to define an asynchronous function.
      SoapQuery: function (args, callback) {
        // do some work
        callback({
          'ResultCode': 0,
          'ResultDesc': "sdfds",
        });
      }
    }
  }
};

目前,当收到如下请求时:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Body>
        <app:SoapQuery xmlns:app="http://app.com/app">
            <TransType>11124</TransType>
        </app:SoapQuery>
    </soapenv:Body>
</soapenv:Envelope>

它返回:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope
  xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:app="http://app.com/app">
  <soap:Body>
    <app:SoapQueryResponse>
      <app:ResultCode>0</app:ResultCode>
      <app:ResultDesc>sdfds</app:ResultDesc>
    </app:SoapQueryResponse>
  </soap:Body>
</soap:Envelope>

但我希望响应看起来像:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope
  xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:app="http://app.com/app">
  <soap:Body>
    <app:SoapQueryResult> <!-- not - SoapQueryResponse -->
      <ResultCode>0</ResultCode>  <!-- notice there is no `app:` -->
      <ResultDesc>sdfds</ResultDesc> <!-- notice there is no `app:` -->
    </app:SoapQueryResult>
  </soap:Body>
</soap:Envelope>

我尝试了不同的方法,但似乎都没有对响应类型产生影响。我觉得我在 WSDL 或处理程序中遗漏了一些东西..

【问题讨论】:

    标签: javascript node.js soap wsdl node-soap


    【解决方案1】:

    在您的 WSDL 定义中省略 RPC 标记并将其更改为

    &lt;soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/&gt;

    &lt;soap:binding transport="http://schemas.xmlsoap.org/soap/http" /&gt;

    因为您使用RPC 作为样式,它会将app: 添加到输出消息和部分,忽略您的outputName 并将其替换为SoapQueryResponse。删除RPC 标签将为您提供此输出

    <?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope
      xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
      xmlns:app="http://app.com/app">
      <soap:Body>
        <SoapQueryResult> <!-- notice there is no `app:` -->
          <ResultCode>0</ResultCode>  
          <ResultDesc>sdfds</ResultDesc>
        </SoapQueryResult>
      </soap:Body>
    </soap:Envelope>
    

    默认情况下,node-soap 将从消息和消息部分中删除所有 targetNamespace 前缀,如果不是样式化 RPC。我创建了一个拉取请求here,它将允许用户在输出消息上添加可选的targetNamespace,以便在输出上添加前缀而不是部分。因此,根据建议的拉取请求,您可以将 targetNamespace 添加到您的消息中

     <message name="SoapQueryResult" targetNamespace="app">
          <part name="ResponseCode" type="xsd:string"/>
          <part name="ResultDesc" type="xsd:string" />
     </message>
    

    你会得到你想要的输出

    <?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope
      xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
      xmlns:app="http://app.com/app">
      <soap:Body>
        <app:SoapQueryResult>
          <ResultCode>0</ResultCode>
          <ResultDesc>sdfds</ResultDesc>
        </app:SoapQueryResult>
      </soap:Body>
    </soap:Envelope>
    

    【讨论】:

    • 从您的回购中提取,它似乎工作正常......谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-17
    • 2019-01-28
    • 2020-09-23
    相关资源
    最近更新 更多