| home / programming / coldfusion / 2 | [previous] [next] |
|
|
One of the interesting features of web services is the ability to create custom datatypes. In the XMethods Query Service example from the section on consuming web services, we dealt with a web service that returned an arbitrary datatype called ServiceSummary. If you recall, the WSDL for the complex type looked like this:
<complexType name="ServiceSummary"><sequence><element name="name" nillable="true" type="xsd:string"/><element name="id" nillable="true" type="xsd:string"/><element name="shortDescription" nillable="true" type="xsd:string"/><element name="wsdlURL" nillable="true" type="xsd:string"/><element name="publisherID" nillable="true" type="xsd:string"/></sequence></complexType>
ColdFusion MX also has a mechanism for creating arbitrary datatypes.
This allows you to create your own datatypes to use as input or output parameters
within your ColdFusion-based web services. To create an arbitrary datatype,
you create a shell CFC for the datatype and define it using cfproperty
tags. For example, to create a complex type called EmployeeDetail,
you would create a CFC that looks like Example 24-9.
Example 24-9: Defining a custom datatype as a CFC
<cfcomponent><cfproperty name="name" type="string"><cfproperty name="title" type="string"><cfproperty name="department" type="string"><cfproperty name="phoneExt" type="numeric"></cfcomponent>
The file should be saved as EmployeeDetail.cfc
in a directory under your web root. Once you have the EmployeeDetail.cfc
file created, you can use it within your other CFCs as a custom datatype. It
can be used as a value for the type attribute of
the cfargument tag (as a type for an input parameter)
or in the returntype attribute of the cffunction
tag (as a type for an output parameter). For example, to use EmployeeDetail
as a datatype for an input parameter, your CFC code would look like that in
Example 24-10.
Example 24-10: Web service using a custom datatype as both input and output parameter
<cfcomponent><cffunction name="addEmployee" access="remote"returntype="EmployeeDetail" output="no"><cfargument name="employee" type="EmployeeDetail" required="yes"><cfquery name="getEmployees" datasource="programmingcf">insert into employeeDirectory (name,title,department,phoneExt)values ('#arguments.employee.name#', '#arguments.employee.title#','#arguments.employee.department#',#arguments.employee.phoneExt#)</cfquery><cfreturn arguments.employee></cffunction></cfcomponent>
You'll need to save this CFC as employee.cfc,
preferably in the same directory as the EmployeeDetail.cfc
file. When called as a web service, this CFC accepts an input parameter of type
EmployeeDetail. It then uses the data in that data
structure to perform an insert query in the EmployeeDirectory
table of our example database. Next, the same EmployeeDetail
data structure is returned back to the client that called the web service. The
code to call the web service is shown in Example 24-11.
Example 24-11: Invoking a ColdFusion-based web service and passing in a custom datatype
<cfset myEmployee = structNew( )><cfset myEmployee.name = "Joe Blow"><cfset myEmployee.title = "Guy Friday"><cfset myEmployee.department = "Administrative"><cfset myEmployee.phoneExt = "1323"><cfinvokewebservice="http://localhost/programmingcf/ch24/employee.cfc?wsdl"method="addEmployee"returnvariable="aEmployeeDetail"><cfinvokeargument name="employee" value="#myEmployee#"/></cfinvoke><cfdump var="#aEmployeeDetail#"><cfoutput>#aEmployeeDetail.name#</cfoutput>
The EmployeeDetail datatype is coded
as a ColdFusion structure. The web service is invoked, and the EmployeeDetail
datatype passed along as an input parameter. Executing the code in Example 24-11
results in the output shown in Figure 24-10. Notice that the variable aEmployeeDetail
is a complex type, not a true ColdFusion structure.
|
|
| home / programming / coldfusion / 2 | [previous] [next] |
Created: March 27, 2003
Revised: Sept. 1, 2003
URL: http://webreference.com/programming/coldfusion/2