Creating Webservices with Variable Arguments and Other Gotcha's in Coldfusion

Coldfusion makes building webservices very easy. You can quickly create a component and set the function you wish expose by setting the function access to remote and you are done!


Webservices are a bit quirky though and what worked as a component call won’t always work as a webservice call. Below are some of the issues I had to work though. Hopefully this saves time for someone else.

Webservice functions cannot have optional arguments.

Unlike a function call in a component, all arguments must be passed in a webservice call. If you do not pass the exact number of arguments with the correct type you will get an error that looks something like this:

coldfusion.xml.rpc.ServiceProxy$ServiceMethodNotFoundException: Web service operation getItems with parameters {} cannot be found

I was once building a webservice to search on multiple terms and for a moment was faced with the prospect that I would need to build multiple versions of the same function to search on a single term, or two, or three, etc. The solution to this problem is for your webservice to take a single structure as its only argument.



Inside the myArguments structure you can now pass as many or few values as you’d like.

If you’ve built your webservice as a public facade for an underlying service you can now just pass your webservice argument struct to that service as the argumentCollection. The service layer can then work normally and have multiple optional arguments if you’d like since it will receive the webservice’s single structure argument as its argumentCollection.




Webservices are cached in the Coldfusion Administrator

This is not an issue once the webservice is deployed, but during development you may find that your code updates don’t seem to be changing anything. This is because the old wsdl information of the webservice gets cached in the CFadmin. You must log into CFAdmin and manually delete it from the webservice registry. It will re-register itself once you use it again.

Webservices have data limitations that Coldfusion Components do not have

My co-worker & I were once getting an error from a webservice that was making us pull our hair out. The function worked fine when called as a component, but once called as a webservice the same data would throw this error:

The fault returned when invoking the web service operation is:AxisFault faultCode: { http://schemas.xmlsoap.org/soap/envelope/ }Server.userException faultSubcode: faultString: java.lang.NumberFormatException: Invalid date fault

Eventually we discovered that in the test data there was a date with a value of “9999” for the year. This date is not out of range for Coldfusion, but when the call is converted to a webservice the date is out of range for Apache Axis and the request puked.

Converting arrays, structures, queries, and components to XML in Coldfusion

Back in 2008 I needed a way to convert mixed CF data into XML documents. I had seen Ray Camden’s “toXML” script, but the functionality was too limited for me. I had to write my own from scratch that not only gave me full control over the creation of the XML document, but also allowed me to send in all of the Coldfusion datatypes, including cfc components. The results was AnythingToXML.

Example Code:






#myXML#

The results would look something like:



    Doll
    $10.00


    Game
    $16.00


    Puzzle
    $12.00


If you structure your data and then pass it to AnythingToXML along with the optional Root Node name and optional list of XML attributes. AnythingToXML will convert your data to a complex and well-formatted XML documents. It will accept any combination of Arrays, Queries, Structures, Objects, and Simple Types as input.

Future upgrades include adding schema and namespace support.

You can download AnythingToXML Here
More Examples of using AnythingToXML are Here

WordPress Source Code Syntax Highlighting for Coldfusion

If I’m going to blog about code, I need a good syntax highlighter for WordPress. Searching through the many plug-ins available its obvious that there are really only two choices: Plugins that use Google’s Prettify and Plug-ins that use GeSHi.

I first tried the “Google Syntax Highlighter for WordPress” plug-in only to find that it doesn’t do Coldfusion. I downloaded someone’s plug-in for Google’s plug-in that would do Coldfusion Syntax highlighting only to have it start breaking the page layout. There was also a noticable slow down in the page loading. I gave up after spending my morning trying to get this to work.

The next plug-in I tried was “WP-Syntax” which is based on GeSHi. Not only did it support Coldfusion right out of the box, but it worked perfectly the first time I used it! If anyone else needs Coldfusion Source Code Syntax Highlighting for WordPress I highly recommend checking out the “Wp-Syntax” plug-in.

Example:
(Code box begins with <pre lang=”cfm”>)


The other plug-in you’ll probably need is the “Raw-HTML” plug-in. This will help keep WordPress from being helpful and scrambling up your code. Unfortunately this doesn’t solve the issue of switching between the HTML tab to the Visual tab. If you do that WordPress will still eat your code.