Creating ColdFusion services for client applications

You can create applications with Flex that access services from a wide variety of technologies. These technologies include:

  • ColdFusion

  • PHP

  • HTML (REST-style) services

  • Web services (SOAP)

After creating a service, it is a good idea to test the service before accessing it from a client application.

The principles of creating and testing ColdFusion services can be applied to other service technologies.

Create a ColdFusion component

Create a ColdFusion component (CFC) that can be accessed as a service by Flex over the Internet.

  1. Code the following CFC:

    <cfcomponent> 
        <cffunction name="getMessage" access="remote" returntype="string"> 
            <cfset message = "Hello from CF!"> 
            <cfreturn message> 
        </cffunction> 
    </cfcomponent>

    The function getMessage() is a service operation that client applications access.

    Make sure to specify remote as the access type for the function.

  2. Save the CFC as HelloService.cfc in your web root. Place it in a folder named HelloCF.
    <CF Web Root>/HelloCF/HelloService.cfc
  3. In HelloService.cfc, add operations that return localized messages and locales.

    <cffunction name="getLocalizedMessage" returnType="string" access="remote"> 
        <cfargument name="locale" required="yes"/> 
             
        <cfswitch expression="#locale#"> 
            <cfcase value="en"> 
                <cfset message = "Hello from CF!"/> 
            </cfcase> 
            <cfcase value="es"> 
                <cfset message = "Hola de CF!"/> 
            </cfcase> 
            <cfcase value="ne"> 
                <cfset message = "Namaskar CF bata!"/> 
            </cfcase> 
            <cfdefaultcase> 
                <cfthrow message="Unknown locale code"/> 
            </cfdefaultcase> 
        </cfswitch> 
        <cfreturn message> 
    </cffunction> 
    <cffunction name="getLocales" returnType="array" access="remote"> 
        <cfset codes = listToArray("en,es,ne")/> 
        <cfreturn codes> 
    </cffunction>

    The getLocalizedMessage() function specifies an argument of type string. The getLocales() function returns an array of strings.

Test the ColdFusion service

There are various ways to test the service before you access it from an application.

  1. Create a ColdFusion script file, helloservicetest.cfm, that calls the service and displays the returned data in HTML.

    Use the cfinvoke tag to call the service.

    Use the cfdump tag to view the results.

    Testing getMessage()... 
    <cfinvoke component="HelloService" method="getMessage" returnvariable="message"/> 
    <p> Result: <cfdump var="#message#"/> </p> 
     
    Testing getLocales()... 
    <cfinvoke component="HelloService" method="getLocales" returnvariable="locales"/> 
    <p> Result: <cfdump var="#locales#"/> </p> 
     
    Testing getLocalizedMessage("es")... 
    <cfinvoke component="HelloService" method="getLocalizedMessage" locale="es" returnvariable="message"/> 
    <p> Result: <cfdump var="#message#"/> </p>
  2. Save helloservicetest.cfm in the same folder as HelloService.cfc, and call the script from a web browser:

    http://localhost:8500/HelloCF/helloservicetest.cfm
    View full size graphic
    Using cfinvoke and cfdump

    If there is an error in the service, ColdFusion displays detailed information about the error.

    You can also enable Robust Exception Information for the ColdFusion server to get detailed debugging output.

  3. Add trace statements to the service.

    In the ColdFusion Administrator, enable debugging and logging.

    Add ColdFusion trace statements as necessary to debug your service. For example, add the following line in helloservicetest.cfm:

    Testing getMessage()... 
    <cfinvoke component="HelloService" method="getMessage" returnvariable="message"/> 
    <cftrace category="getMessage End" inline="yes" var="message" text="GetMessage call has completed"> 
    . . .

    Call the script from a web browser to view the debugging output. You can also inspect the ColdFusion log files.

    View full size graphic
    Testing a service directly from a URL

  4. Call a function in the service directly from a URL.

    For example, call getLocalizedMessage() with the parameter es:
    http://localhost:8500/HelloCF/HelloService.cfc?method=getLocalizedMessage&locale=es
    View full size graphic
    Testing a service directly from a URL