17 January 2011

I have a SOAP based web service that I wrote when ColdFusion MX 6 first came out, and it does not work in BlueDragon.NET.

The web service is called a quote from Mr. Spock. (Yes, I am a GEEK.) It has a one method called getQuote which returns a coldFusion structure or a HashMap object if you are using Java. When I test my client for the web service in BlueDragon, it retuned an empty object. So I decided to rewrite my web service code to return a simple object instead. My current cfc for the web service looks like this;

<cfcomponent displayName="spock"> <cffunction access="private" name="getRand" returntype="numeric"> <cfset randNum = randrange(1,85)> </cffunction> <cffunction access="Remote" name="getQuote" returntype="struct" hint="Get Quote from Mr. Spock"> <cfquery name="myQuoteQuery" datasource="#application.davidsDSN#"> Select * From SpockQuotes WHere QuoteNum = #getRand()#; </cfquery> <cfset spock = structnew()> <cfset spock.quote = trim(myQuoteQuery.quote)> <cfset spock.Episode = myQuoteQuery.episode> <cfset spock.stardate = myQuoteQuery.stardate> </cffunction> </cfcomponent>

ColdFusion MX has a built in way to describe simple objects in the WSDL file. If you create another cfc using the cfproperty tag, you can return that type in the web service.

My structure has three name/value pairs for the quote, stardate and episode. The values are all of type string. So the following cfc that describes the simple object looks like this;

<cfcomponent displayname="SpockQuoteModel"> <cfproperty name="quote" type="string" /> <cfproperty name="episode" type="string" /> <cfproperty name="stardate" type="string" /> </cfcomponent>

Then I wrote a new web service code that uses this cfc;

<cfcomponent displayName="SpockWS"> <cffunction access="private" name="getRand" returntype="numeric"> <cfset randNum = randrange(1,85)> </cffunction> <cffunction access="Remote" name="getQuote" returntype="SpockQuoteModel" hint="Get Quote from Mr. Spock"> <cfquery name="myQuoteQuery" datasource="#application.davidsDSN#"> SELECT quote, episode, stardate FROM SpockQuotes WHERE QuoteNum = <cfqueryparam cfsqltype="cf_sql_integer" value="#getRand()#" />; </cfquery> <cfset spock = createObject("component","SpockQuoteModel") /> <cfset spock.quote = trim(myQuoteQuery.quote) /> <cfset spock.Episode = myQuoteQuery.episode /> <cfset spock.stardate = myQuoteQuery.stardate /> </cffunction> </cfcomponent>

I tested this in BlueDragon.NET with the following client code;

<cfinvoke webservice="[http://www.fekke.com/com/SpockWS.cfc?wsdl](http://www.fekke.com/com/SpockWS.cfc?wsdl)" method="getQuote" returnvariable="aHashMap"> </cfinvoke> </font> Quote: "#aHashMap.quote#"
Episode Name: </font>#aHashMap.episode#</strong>
Stardate: </font>#aHashMap.stardate#</em>
</cfoutput></div> It Worked!!! I will copy this sample code to my web site with instructions soon.


blog comments powered by Disqus