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!