<

Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x

Creating and Consuming Web Service in ColdFusion

Published on
14,676 Points
4,976 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
0 Comments

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Join & Write a Comment

Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month