CFComponent Web服务 - 获取SOAP属性

4

以下是一个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 web服务:

<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和Web服务,对于如何从<testService>中提取属性a1a2一无所知,在谷歌上搜索了一下,但没有找到任何相关参考资料。有什么想法吗?
===编辑===
想着附上类型定义可能会有用:
<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>

请注意,尽管这是我的测试网络服务,但它基于我们合作伙伴提供的数据架构,这意味着我的网络服务必须符合它。
=== 解决方案 ===
根据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>
3个回答

1
你只需要解析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#">

我可能错了,但我认为你谈论的与cfcomponent web服务无关,因为在你的例子中,你有XML文档可以使用,但我认为在使用cfcomponent web服务时不是这种情况,或者我可能错了,请纠正我如果是这种情况。 - ndlu
我错了,看了Gerry的最新帖子后,我现在明白你当时想说什么了(就像我说的,我对Coldfusion和Web服务还很陌生)。谢谢。 - ndlu

1
根据您的评论,我认为您需要使用getSoapRequest()函数,然后使用keshav-jha提供的代码进行解析。请保留HTML标签。
<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>

谢谢Gerry,我不知道GetSOAPRequest()函数。虽然它不是我期望的内置函数,但它可以完成工作 :) - ndlu

0

如果您正在创建Web服务,则完全控制如何使用Web服务。在这种情况下,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>

SOAP UI很棒,我一直在使用它。数据模式是由我们的合作伙伴定义的,我的任务是创建Web服务,因此不能像您建议的那样更改为元素。 - ndlu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接