Publishing Web Services with ColdFusion

My last blog dealt with consuming web services with ColdFusion.  In this entry I will discuss how to publish a web service using ColdFusion.  To reiterate, my knowledge going into this subject was limited, but once again ColdFusion has made a complex topic quite easy to grasp.  In fact, I had actually been using the web service publishing capabilites of ColdFusion for a little while without even realizing it.  Let me show you just how easy it is. 
 
To create / publish a web service, you must start by defining a component, and like any component it may have one or more functions.  Let’s look at this simple Hello World example:
 
     <cfcomponent>
 
          <cffunction name="init" returntype="string">
               <cfreturn "Hello World">
          </cffunction>    
 
     </cfcomponent>
 
So we have a simple component that defines one function name init that returns the string "Hello World".  To turn this into a web service, consumable by any language that supports it, is add ONE attribute to the function:
 
     <cfcomponent>
 
          <cffunction name="init" returntype="string" access="remote">
               <cfreturn "Hello World">
          </cffunction>    
 
     </cfcomponent>
 
All I have done here is added the access attribute to the function and specified its value as remote.  That’s it!  We now have a fully function web service with a method that will return a string.  Assuming this component is named myFirstWebService and it lives directly on your web root, you can call it directly like the following:
 
 
Doing so will produce an html output page featuring descriptions of the web service and its functions from the self documenting features of ColdFusion components.
Add ?WSDL to the end of the URL and you will see the WSDL definition of the service, which is the URL that can be used to consume your brand new service.  And that is it!
 
I hope this brief introduction can help you get started in publishing your own services.  Next time I will discuss object creation in ColdFusion and how to use Objects in Web Services, as well as modeling complex types with structs in ColdFusion.
 
Happy Coding!
 

Consuming Web Services with ColdFusion

The agency I work for is implementing the Avatar clinical software system from Netsmart Technologies.  Netsmart has recently released a collection of Web Services for the Avatar system to extend it’s functionality.   It became my duty to fully leverage these Web Services using my favorite language, ColdFusion.  Up until this point I have never worked with a Web Service or even fully understood what one is.  So, I put on my learnin’ hat and started searching around the web for some information.  I soon learned that a web service, loosely and briefly designed can be summized as a way for two software systems, written in different languages and running on different platforms, to talk to each other using a standardized language they can both understand over a protocol they can both understand.  The language in this case is XML (extensible markup language) and the protocol is HTTP (hypertext transfer protocol).  Okay, so now that I know what it is, how can I use it?  A little bit more research and I found out that ColdFusion makes using a Web Service, which is formally referred to as "Consuming" the service, extremely simple.  In fact, ColdFusion has two equally simple ways to consume: using the <cfinvoke> tag or the createObject() function with the "webservice" argument. 
 
The tag based solution works like the following:
 
   <cfinovke webservice="urlToTheWSDL" method="nameOfMethod>
        <cfinvokeargument name="argumentName" type="argumentType">
                                                .
                                                .
                                                .
    </cfinvoke>
 
For the webservice attribute, all you need to do is specify the URL to the WSDL file of the web service.  Every web service you will consume has an accompanying WSDL (Web Service Defintion Language) file that describes what the web service does, what its methods / functions are , and what data types it takes in and returns.  To call a specfic function of the web service, just supply its name in the method attribute.  If the method takes in arguments, you can specify those using <cfinvokeargument> tags, supplying the name of the argument as well as the type.  The type can be simple (string, numeric, boolean,…) or complex (struct, array, user defined object, array of object, …).  If the web service returns a value, you can use the returnvariable attribute of the <cfinvoke> tag to capture it for further processing. 
 
The function based solution looks like this:
 
     <cfset ws = createObject("webservice", "urlToTheWSDL")>
     <cfset returnValue =  ws.nameOfMethod(arg1Name = arg1Value, arg2Name = arg2Value, …)>
 
  or using scripting…
 
     <cfscript>
          ws = createObject("webservice", "urlToTheWSDL");
          returnValue = ws.nameOfMethod(arg1Name = arg1Value, arg2Name = arg2Value, …);
     </cfscript>
         
All you do here is create a web service object, then call methods of the object passing name / value pairs to it.  While this second method requires two seprate steps to consume the service, I prefer it as I believe it has a cleaner appearance when there are multiple arguments to send to the method. 
 
As I hope you can see, ColdFusion makes working with the basic functionality of Web Services quite painless.  In later posts, I will examine how to create, or publish, a Web Service using ColdFusion, as well as dealing with complex data types that are often used in Web Services.
 
Hope this information is useful.  Happy Coding!