cfcomponent Web 服务 - 获取 SOAP 属性

cfcomponent web service - getting SOAP attributes

这是一个 SOAP 请求示例:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:ws="http://ws_test">
   <soapenv:Header/>
   <soapenv:Body>
      <ws:testService a1="a1" a2="a2">
         <ws:e1>e1</ws:e1>
         <ws:e2>e2</ws:e2>
      </ws:testService>
   </soapenv:Body>
</soapenv:Envelope>

这是我的示例 cfc 网络服务:

<cfcomponent style="document" wsversion = 1>
    <cffunction name="testService" returntype="String" access="remote" >
        <cfargument type="string" name="e1"/>
        <cfargument type="string" name="e2"/>

        <!--- Missing: code to extract a1 and a2 --->   

        <cfreturn "#e1# #e2#">
    </cffunction>
</cfcomponent>

我是 Coldfusion 和网络服务的新手,我不知道如何从 a1a2 =31=]<testService>,用谷歌搜索但找不到任何参考。有什么想法吗?

===编辑===

如果我附上类型定义,我认为它可能会有用:

<complexType name="testServiceType">
    <sequence>
        <element name="e1" type="string"></element>
        <element name="e2" type="string"></element>
    </sequence>
    <attribute name="a1" type="string"/>
    <attribute name="a2" type="string"/>
</complexType>

请注意,虽然这是我的测试 Web 服务,但它基于我们合作伙伴提供的数据架构,这意味着我的 Web 服务必须符合它。

===分辨率===

根据 Gerry 的回答,这就是我最终做的事情:

<cfcomponent style="document" wsversion = 1>
    <cffunction name="testService" returntype="String" access="remote" >
        <cfargument type="string" name="e1"/>
        <cfargument type="string" name="e2"/>

        <cfset soapReq = getSOAPRequest()>
        <cfset soapXML = xmlParse(soapReq)>
        <cfset attributes = soapXML.Envelope.body.XmlChildren[1].XmlAttributes>

        <cfset a1 = attributes.a1>
        <cfset a2 = attributes.a2>

        <cfreturn "#e1# #e2# #a1# #a2#">
    </cffunction>
</cfcomponent>

你只需要解析你的XML然后得到a1和a2的值

<cfsavecontent variable="myXML" >
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://ws_test">
   <soapenv:Header/>
   <soapenv:Body>
      <ws:testService a1="a1" a2="a2">
         <ws:e1>e1</ws:e1>
         <ws:e2>e2</ws:e2>
      </ws:testService>
   </soapenv:Body>
</soapenv:Envelope>
</cfsavecontent>
 <cfset parXML = xmlParse(myXML) />
<cfdump var="#parXML.Envelope.body.XmlChildren[1].XmlAttributes.a1#">

如果您正在创建网络服务,那么您可以完全控制网络服务的使用方式。在这种情况下,a1 和 a2 永远不会传递到 CFC 进行处理。所以如果它们有意义,你可以将它们设置为像e1和e2这样的参数。

我曾经用来理解和使用 Web 服务的最佳工具之一是 SoapUI。如果您创建 example.cfc 并将 SOAPUI 指向它,您将得到一个 XML 请求,如下所示:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="http://soap.stack">
   <soapenv:Header/>
   <soapenv:Body>
      <soap:testService>
         <soap:e1>e1</soap:e1>
         <soap:e2>e2</soap:e2>
      </soap:testService>
   </soapenv:Body>
</soapenv:Envelope>

如果要处理a1和a2,没有理由不将它们作为常规参数处理。

因此您可以制作如下所示的 CFC:

<cfcomponent style="document" wsversion = 1>

    <cffunction name="testService" returntype="String" access="remote" >
        <cfargument type="string" name="a1"/>
        <cfargument type="string" name="a2"/>
        <cfargument type="string" name="e1"/>
        <cfargument type="string" name="e2"/>


        <cfset var ret=serializeJSON(arguments) />
        <cfreturn "#ret#">
    </cffunction>
</cfcomponent>

如果您将 SOAPUI 指向它,那么它将生成一个 SOAP 信封,如下所示:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="http://soap.stack">
   <soapenv:Header/>
   <soapenv:Body>
      <soap:testService>
         <soap:a1>?</soap:a1>
         <soap:a2>?</soap:a2>
         <soap:e1>?</soap:e1>
         <soap:e2>?</soap:e2>
      </soap:testService>
   </soapenv:Body>
</soapenv:Envelope>

根据您的评论,我认为您需要 getSoapRequest() 然后使用 keshav-jha 给出的答案中的代码解析它

<cfcomponent style="document" wsversion = 1>

    <cffunction name="testService" returntype="String" access="remote" >
        <cfargument type="string" name="e1"/>
        <cfargument type="string" name="e2"/>
        <cfscript>
            soapReq=GetSOAPRequest();
            soapXML=xmlParse(soapReq);
            bodyAttributes = {
                a1:soapXML.Envelope.body.XmlChildren[1].XmlAttributes.a1
                ,a2:soapXML.Envelope.body.XmlChildren[1].XmlAttributes.a2
            };
            return serializejson(bodyAttributes);
        </cfscript>

    </cffunction>
</cfcomponent>