spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / coldfusion / 2 To page 1To page 2current pageTo page 4
[previous] [next]

Programming ColdFusion MX: Web Services

Java Software Engineer / Architect Sr (IL)
Next Step Systems
US-IL-Chicago

Justtechjobs.com Post A Job | Post A Resume
Developer News
OpenOffice 3.2 Lands Amid Critical Changes
Red Hat, IBM Firmly in KVM Virtualization Camp
Red Hat Talks Up Open Source Cloud Plans


Creating Custom Datatypes

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">
   
<cfinvoke 
 webservice="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.

Figure 24-10. Passing and returning a custom datatype

 


home / programming / coldfusion / 2 To page 1To page 2current pageTo page 4
[previous] [next]


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

webref The latest from WebReference.com Browse >
Search Engine Optimization: Selecting and Embedding Keywords · Are Google's Language Translation Web Services Ready for Prime Time? · Installing and Using Meeplace, the Business Review CMS
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
IBM DB2 10 for z/OS: Justifying the Upgrade · Living La Vida Colo: Choosing the Right Colocation Facility · FTC Concerns over Social Media Privacy Linger

Created: March 27, 2003
Revised: Sept. 1, 2003

URL: http://webreference.com/programming/coldfusion/2