Avg. Rating 5.0
Tags:



Problem

The rest of the internet is in love with using REST to deliver web services instead of SOAP. How can I implement RESTful web services with ColdFusion?

Solution

Don't roll your own! Depending on your preference, use Taffy if you prefer a convention-based solution or PowerNap if you prefer a configuration-based solution. Also, frameworks like Coldbox and Mach-ii have built-in endpoints for RESTful web services too.

Detailed explanation

There are (at least) three general approaches to implementing RESTful web services in ColdFusion. The first two (convention and configuration) are independent of your application framework (should you be using one) so you can choose one that best suits your preferences. That's not to say you can't use one of them with your framework of choice (even one with built-in REST support) ~ your service tier should be independent of framework dependencies anyway. 

That said, here are some suggestions that save you the pain of rolling your own REST wrappers:

Convention Approach:

Taffy ( http://taffy.riaforge.org) is one of the more recent examples that has shown itself to be both clever and simple to implement. Adam Tuttle posted a nice introduction to Taffy on his blog ( http://fusiongrokker.com/post/taffy-a-restful-framework-for-coldfusion). One thing you should be somewhat comfortable with is writing script-based CFCs, but it's a fairly simple transition. 

Taffy uses function metadata to know what to invoke when a specific URL pattern is matched. 

For example:

component
   extends="taffy.core.resource"
   taffy_uri="/{username}/status/{tweetId}" {...}

This defines a CFC as a handler for any URLs matching the pattern of some username, the string status, and a variable tweet id.  Within the CFC, the functions map to the standard HTTP verbs (GET, POST, etc.). 

Configuration Approach:

PowerNap  http://powernap.riaforge.org/ is a simple, approachable REST framework worth your time if you like to have all of the URL mappings visible in one spot.  

You'll define a CFC (extending powernap.core.Engine) and within the init method you'll have many lines like this:

<cfset map().get().uri("/say/something/now/{textToSpeak}").to("world").calls("saySomething") />

So now any URLs matching the pattern will now invoke the "saySomething" function in the "world" CFC, passing the appropriate arguments along. 

Framework Support:

*ColdBox 3 makes it really, really easy to build in support as previewed in Luis Majano's post here ( http://blog.coldbox.org/post.cfm/coldbox-rest-enabled-urls) and shown here in the release docs  http://wiki.coldbox.org/wiki/WhatsNew:3.0.0.cfm#SES_Interceptor_Updates

*Mach-ii 1.9 will have REST endpoint support baked in - currently still being discussed on the BER Google Group ( http://groups.google.com/group/mach-ii-developers) with more to come soon. Available right now in the 1.9 milestone 2 release (which is currently usable, as at least one group has it in production already).

Conclusion:

I'm sure there are other options out there, but these are the simplest ones to get started with. While it's not as simple as setting "access=remote" in a CFC, any of these approaches will get you going very quickly.


+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes