The amazing adventures of Doug Hughes

This is a quick blog entry to answer a question I received by email. Here’s the question:

I am a final year Computer Science and Engineering student. I have planned to implement and deploy a Web Service for my final year project. I couldn’t find a good tutorial for creating a Web Service with ColdFusion. It would be very kind of you if you can suggest me what to do, or if you could refer me to some tutorial which is available for download.

Honestly, I debated whether or not to blog this answer. This is the kind of topic that bloggers were writing about when ColdFusion MX came out many years ago. That said, sometimes old topics need to be rehashed for those newer to ColdFusion. (In other news, there are programmers who are new to ColdFusion!) Anyhow, to actually answer the question, creating web services in ColdFusion is painfully simple. All you do is start out by writing a CFC. For example, this hello world CFC:

<cfcomponent>
    <cffunction hint="I say hi!" name="sayHello" returntype="string">
        <cfargument default="world" hint="Whom to say hi to." name="to" required="false" type="string"/>
        <cfreturn "Hello, #arguments.to#"/>
    </cffunction>
</cfcomponent>

This component, should you instantiate it and call the sayHello method will return a string saying hello to whatever you tell it to. The question of the moment is how to make a web service that will remotely expose this method? By default, all functions in ColdFusion components are public. This means any code in your application can instantiate the component and call the method. Other options are private, protected, and remote. I’m going to ignore private and public and hone in on remote. If you set the access to remote you are allowing anyone in the word to execute that method as a web service. Here’s the updated code:

<cfcomponent>
    <cffunction access="remote" hint="I say hi!" name="sayHello" returntype="string">
        <cfargument default="world" hint="Whom to say hi to." name="to" required="false" type="string"/>
        <cfreturn "Hello, #arguments.to#"/>
    </cffunction>
</cfcomponent>

That’s it! That’s all it takes. To actually use this web service simply browse to the path to the CFC over the web and append “?wsdl” to the end of the request. For example: http://somesite.com/helloWorld.cfc?wsdl. At this point you have a WSDL file and can use that for remote requests.

Tag Cloud

<span>%d</span> bloggers like this: