
1109
DEVELOPING
COLDFUSION 9 APPLICATIONS
Using Web Elements and External Objects
Las
t
upda
te
d 8/5/2010
<cfscript>
ws = CreateObject("webservice", "http://localhost/echo.cfc?wsdl");
wsresults = ws.echoString("hello");
writeoutput(wsresults);
</cfscript>
Using ColdFusion components to define data types for web services
ColdFusion lets you define components that contain only properties. Once defined, you can use components to define
data types for web services. The following code defines a component in the file address.cfc that contains properties that
represent a street address:
<cfcomponent>
<cfproperty name="AddrNumber" type="numeric">
<cfproperty name="Street" type="string">
<cfproperty name="City" type="string">
<cfproperty name="State" type="string">
<cfproperty name="Country" type="string">
</cfcomponent>
The following code defines a component in the filename.cfc that defines first and last name properties:
<cfcomponent>
<cfproperty name="Firstname" type="string">
<cfproperty name="Lastname" type="string">
</cfcomponent>
You can then use address and name to define data types in a ColdFusion component created to publish a web service,
as the following example shows:
<cfcomponent>
<cffunction
name="echoName" returnType="name" access="remote" output="false">
<cfargument name="input" type="name">
<cfreturn #arguments.input#>
</cffunction>
<cffunction
name="echoAddress" returnType="address" access="remote" output="false">
<cfargument name="input" type="address">
<cfreturn #arguments.input#>
</cffunction>
</cfcomponent>
Note:
If the component files are not in a directory under your web root, create a web server mapping to the directory that
contains them. You cannot use ColdFusion mappings to access web services.
The WSDL file for the web service contains data definitions for the complex types name and address. Each definition
consists of the elements that define the type as specified in the ColdFusion component file for that type. For example,
the following example shows the definition for name:
<complexType name="name">
<sequence>
<element name="firstname" nillable="true" type="soapenc:string"/>
<element name="lastname" nillable="true" type="soapenc:string"/>
</sequence>
</complexType>
You can also specify an array of CFCs in the
returnType
attribute, as the following example shows: