ColdFusion 编写WebService 示例

  1.开发 Web Services,编写cfcdemo.cfc组件,代码如下:  

<cfcomponent style ="document"
namespace = "http://www.mycompany.com/"
serviceportname = "RestrictedEmpInfo"
porttypename = "RestrictedEmpInfo"
bindingname = "myns:RestrictedEmpInfo"
displayname = "RestrictedEmpInfo"
hint = "RestrictedEmpInfo">
  <cffunction name = "getEname" access = "remote" returntype="xml" output="no" >
    <cfargument name = "dname" type = "string" required = "false">
    <cfargument name = "loc" type = "string" required = "false">
    <cfquery name = "dt" datasource = "scott">
      select * from dept where
      <cfif arguments.dname neq "">
        dname = #arguments.dname#
      </cfif>
    </cfquery>
    <cfxml variable="Books">
      <XML>
        <cfoutput query="rs">
          <book>
            <bname>#deptno#</bname>
            <isbn>#dname#</isbn>
            <writer>#loc#</writer>
          </book>
        </cfoutput>
      </XML>
    </cfxml>
    <cfreturn Books>
  </cffunction>
</cfcomponent>

 <!---
  ColdFusion开发 Web Services,只需要在ColdFusion组件(.cfc文件)中,
  把需要作为 Web Services 的method的access类型定义为remote就可以了(access="remote")。
 --->

2.测试一下,访问这个cfc组件,注意URL路径后边要加上 ?wsdl 。

eg:http://localhost/mysys/cfcdemo.cfc?wsdl 

显示效果如下:   

   3.开发测试页面,编写webservice.cfm程序调用①开发的 Web service ,代码如下:

   <cfset sWebServiceUrl = "http://localhost/cfcdemo.cfc?wsdl">
    <cfinvoke webservice="#sWebServiceUrl#"

      component = "cfcdemo"

      method = "getdept"

      returnVariable = "dept">
      <cfinvokeargument   name = "dname" value="10"/>   

      <!--- 访问参数,与参数值,对应.cfc中的  cfargument   ---> 
      <cfinvokeargument  name = "loc" value=""/>
    </cfinvoke>
   <cfdump var="#dept#">

4.说明:调用 Web Service 时,webservice.cfm部分和cfcdemo.cfc?wsdl对应如下:

  <cffunction></cffunction>   

  <!--- cfc组件中的方法,在cfcomponent中可以有多个  ---> 

  method -- 对应cfc组件中的方法,<cffunction>标签内name属性的值
  timeout -- 设置请求超时秒数
  returnVariable -- 自定义的变量,里边存放的是 Web Service 返回的值


 <!---传递参数--->                                                            
 <cfinvokeargument name="empno" value="7788"/> --

 <!---接收参数--->

<cfargument  name="empno" type="string" required="true">

 <cfoutput>#dept#</cfoutput> -- 输出 Web Service

<!--- 注意返回类型一定要与 returntype 对应---> 

本文日期2018-11-08

文章参考博客anfslove 

 

 

 

猜你喜欢

转载自www.cnblogs.com/JoinLi/p/9928335.html
今日推荐