<

Creating and Consuming Web Service in ColdFusion

Published on
14,858 Points
5,158 Views
2 Endorsements
Last Modified:
Awarded
A web service is a software related technology that facilitates machine-to-machine interaction over a network.

This article helps beginners in creating and consuming a web service using the ColdFusion Markup Language (CFML) and associated application server.  The article will go through the following steps:

Create a ColdFusion component (CFC).
Create functions within a CFC.
Convert a function to web method to make the CFC a web service.
Publish / test the web service.
Consume the web service.

1. Create a ColdFusion component (CFC)


If you have been developing custom functions and ColdFusion components you are already well on your way to creating a web service.

ColdFusion components are almost OOP based but not totally; they have methods and constructs, and they provide reusable code.



If you know what functions achieve in any programming language, you are not far from understanding components. They turn functions into objects whose methods can be accessed.

The coldfuson component file should be in <cfcomponent>.
<cfcomponent>
      <cffunction name="myFunction"  returntype="string" output="no">
            <cfargument name="myArgument" type="string" required="yes">
            <cfset myResult="Welcome Srikanth">
            <cfreturn myResult>
      </cffunction>
</cfcomponent>

Open in new window

2. Create functions within the CFC


For those who are new to functions in CF.

You can write functions in CFML page:
<cffunction name="myFunction"  returntype="string" output="no">
    <!--- requires a string --->
    <cfargument name="myArgument" type="string" required="yes">
    <cfset myResult=myArgument>
    <!--- returns the same string --->
    <cfreturn myResult>
</cffunction>

Open in new window


And access them as below:
<cfoutput>
   #myFunction("Hi How are you")#
</cfoutput>

Open in new window


Now let's add this is a very simple function which accepts a string and returns the same string to our CFC.
<cfcomponent>
    <cffunction name="myFunction"  returntype="string" output="no">
          <!--- requires a string --->
          <cfargument name="myArgument" type="string" required="yes">
          <cfset myResult=myArgument>
          <!--- returns the same string --->
          <cfreturn myResult>
   </cffunction>
</cfcomponent>

Open in new window

3. Convert a function to web method to make the CFC a web service


This is a quite simple change, as all that is needed is to add access="remote" to our example function.
<!--- ... --->
   <cffunction name="myFunction" access="remote" returntype="string">
<!--- ... --->

Open in new window

4. Publish / test the web service


Now that we have a simple web service with a functioning web method, we need to place this component in an appropriate folder, map it in web server, and test.

To test, we access this file with your browser.  Since this is a component, it should be viewed with the CF component browser (see image 1.2) which will require you to log into CF administrator (see image 1.1) with your admin password.  You will see that MyFunction() is a method that takes a string argument and you can test from there.

5. Consume the web service


<cfinvoke
webservice="http://localhost/path/to/your.cfc?wsdl"
method="MyFunction"
returnvariable="myResults">
<cfinvokeargument name="myArgument" value="HI Srikanth"/>
</cfinvoke>
<cfoutput>
#myResults#
</cfoutput>


<cfscript>
myWebservice = CreateObject("webservice", "http://localhost/path/to/your.cfc?wsdl");
results = myWebservice.MyFunction(myArgument="HI Srikanth");
</cfscript>
<cfoutput>#results#</cfoutput>

Open in new window


Using Createobject  we  can consume our web service.  It takes two parameters: first, type which is webservice in our case; second, the URL to the Web Service Descriptor Language (WSDL) file which can be created by appending the wsdl query string to the CFC file name.
Please find more details about create object here http://livedocs.adobe.com/coldfusion/6/CFML_Reference/functions-pt145.htm


If we registered the webservice in CF administrator then we can use that registered name here; however, we are going with full URL for simple test case.

We can also use cfinvoke to consume web service - Please find more details about cfinvoke here  http://livedocs.adobe.com/coldfusion/6/CFML_Reference/Tags-pt163.htm

With CFMX 7 when invoking the CFC as a web service be careful to name your file something different than the value of the displayname param.

If your file is ViewUsers.cfc do not set displayname="ViewUsers" otherwise you'll receive compile errors.  Also spaces do not make a difference so displayname="View Users" would also generate an error.


We are calling our function Myfunction and passing the parameter "HI Srikanth".
Which will result in printing "HI Srikanth" to the browser.

That's it!  We are done in creating and consuming an example web service.  Hopefully, you found this helpful.
img1.JPG
img2.JPG
2
Comment
0 Comments

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Join & Write a Comment

Did you know PowerShell can save you time with SaaS platforms? Simply leverage RESTfulAPIs to build your own PowerShell modules. These will kill repetitive tickets and tabs, using the command Invoke-RestMethod. Tune into this webinar to learn how…
Wrapper-1-Query. Use an Excel function to calculate a column for an Access query. Part 1. Shows a query in Access that has a calculated column with the results of an Excel worksheet function. See how to call a wrapper function from a query, and …

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month