| home / programming / coldfusion / 2 | [next] |
|
|
This is the conclusion of an excerpt from the book: Programming ColdFusion MX, 2nd Edition by O'Reilly.
Producing (or publishing, as it's sometimes referred to) web services in ColdFusion MX is a very simple proposition. In fact, many people have made the claim that it is the easiest way to create and publish web services. While I won't go that far, I can say that claim is not far from the mark. In essence, you already know everything necessary to publish a simple web service: you learned it back in Chapter 22, ColdFusion Components.
To convert a CFC into a web service, all you need to do is set
to remote the access
attribute of any component methods (cffunction)
you want to expose as web services. That's all there is to it. To show you just
how easy it is to produce a web service in ColdFusion MX, consider the code
shown in Example 24-5. After you've entered the code, save it under webroot\programmingcf\ch24
as helloworld.cfc.
Example 24-5: Producing a simple web service in ColdFusion MX
<cfcomponent><cffunction name="getMessage" access="remote" returntype="string"output="no"><cfargument name="name" type="string" required="yes"><cfreturn "Hello " & arguments.name &"! " & "I've been invoked as a webservice."></cffunction></cfcomponent>
After looking at this code, your first reaction was probably that it looks just like a regular CFC. That's because it is a regular CFC. All that really separates a CFC from a web service is setting the access attribute of whatever method(s) you want to expose to remote. There are a few additional rules for creating web services from CFCs that you need to know about:
returntype specified. If your web service does not return a value, set returntype="void".
output attribute of each cffunction tag should be set to no. Web services are not designed to output directly to a browser. They are, however, designed to return values which can then be output by the client making the call.
cfargument tags with the required attribute set to no, the setting is ignored and the argument treated as required.
Example 24-6 calls the web service you just created, using the
cfinvoke tag just as you have throughout the chapter.
Example 24-6: Calling a web service produced in ColdFusion MX
<cfinvokewebservice="http://localhost/programmingcf/ch24/helloworld.cfc?wsdl"method="getMessage"returnvariable="aString"><cfinvokeargument name="name" value="Rob"/></cfinvoke><cfoutput>#aString#</cfoutput>
Running this example results in the output shown in Figure 24-8.
|
|
| home / programming / coldfusion / 2 | [next] |
Created: March 27, 2003
Revised: Sept 1, 2003
URL: http://webreference.com/programming/coldfusion/2