Blog Author: Doug Hughes

Doug Hughes is a veteran programmer who founded Alagad after briefly flirting with a career in computer animation. Doug has a strong record of achievement with large, complex web applications. His ability to learn and apply new technologies and techniques has helped Doug become one of the web application development industry’s most highly regarded experts.

Doug’s current focus at Alagad is leading an application development team dedicated to helping organizations improve operational efficiency and performance. This commitment to client success has helped Alagad achieve record growth. A frequent speaker at industry conferences and user groups, Doug also regularly contributes to several well-known open source projects. In addition, his articles have been featured in numerous technical publications.

Categories
Blog Archives
Blog RSS Feed

RSS FeedSubscribe to our RSS feed or Atom feed.

The Alagad Technical Team Blog

Creating a Web Service in ColdFusion

Published By: Doug Hughes on Sep 29, 2008 at 07:33 AM
Categories:

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 name="sayHello" hint="I say hi!" returntype="string">
        <cfargument name="to" hint="Whom to say hi to." required="false" default="world" 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 name="sayHello" access="remote" hint="I say hi!" returntype="string">
        <cfargument name="to" hint="Whom to say hi to." required="false" default="world" 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.

0 responses to “Creating a Web Service in ColdFusion”

Leave a Reply