Skip to main content

ColdFusion 10: Working with REST Sub-resources

In REST everything is treated as a resource and each resource is associated with a URI from which it can be accessed. As mentioned in my series of posts on REST; in ColdFusion methods defined in a CFC are made RESTful by adding the attributes httpmethod and restpath. The restpath value specified in the method can then be used to access the resource. Although this would serve the given purpose, it would be a good idea to have a root resource resolve a generic URL and then have different methods that resolve other path of the URL.

For example, consider a generic resource say ‘/orders’. A HTTP PUT request can be sent to update the order (restpath = ‘/orders/{id}’) and a GET request can be sent to retrieve the order details (again restpath=’/orders/{id}’). Now a root resource can resolve the root path ‘/orders’ and then forward the request to another resource to handle the request. The resource that would handle the request is termed as a sub-resource. In order to make a resource as a root resource it should specify the restpath attribute and shouldn’t specify the httpmethod attribute. Also, it should return a reference of a component that would handle the request. Consider this example:


<cfcomponent rest="true" restpath="component1"> <cffunction name="function1" access="remote" returntype="ResourceTwo" restpath="resource1"> <cfset obj = createObject("component", "ResourceTwo")> <cfreturn obj> </cffunction> </cfcomponent>

As observed in the above code snippet, method 'function1' doesn't define the httpmethod attribute but it does define the restpath. Also it returns a reference to a component 'ResourceTwo'. Now this component (ResourceTwo) can handle the request by defining the httpmethod attribute or forward the request to another component by defining the restpath attribute and returninig a reference to the component that would handle the request.


<cfcomponent> <cffunction name="function1" access="remote" returntype="string" httpmethod="GET"> <cfreturn "From ResourceTwo, handling GET request"> </cffunction> <cffunction name="function2" access="remote" returntype="string" httpmethod="POST"> <cfreturn "From ResourceTwo, handling POST request"> </cffunction> </cfcomponent>

As you can see there are two methods defined that handle the request (httpmethod="GET\POST"). Also, the restpath attribute is not defined in these methods.

Now, when a GET request is issued on the restpath '/resource1'; the root resource (function1 in ResourceOne.cfc) would accept the request and then forward the request to a sub-resource (function1 in ResourceTwo.cfc) to handle the request. The sub-resource would handle the request and return the result to the client. If a POST request is issued then the root resource would forward the request to a sub-resource that would handle the POST request (function2 in ResourceTwo.cfc).


  1. Hmm. This is rather interesting. Is it really a subresource though? To the outside world, there is no sub resource. There is just the resource. From the code perspective, yeah, you have some abstraction going on, but "resource" in terms of REST, to me, implies the public facing portion. Would you agree that the terminology here is maybe a bit misleading? Or am I reading it wrong?

  2. Ok, I did a bit of reading - and I can see how this is a bit more complex than I thought. In your example, you just create a static CFC. In the docs, they show creating a unique CFC (well, one with particular properties), and then that can handle the http method.

    I still feel weird calling this s subresource though. I mean, /rest/student/5 as a URI would be the same whether or not you use this technique. Right?

  3. Think of it this way - there is one public facing REST service which lists the entire model - customer, orders etc,. There can be two methods that handle the path '/orders' and '/customers'. The sub-resources can take care of crud operations on these entities.It actually can be viewed as a controller that directs the request to a component that can service the request.

    It is not always necessary to organize the code with sub-resources i.e. there can be one method that can handle /rest/student/5 to handle the request.

  4. I guess what's bugging me is that we define Resource as something public facing. Subresources though are entirely server side. The client never knows how we actually implemented it. There is a total disconnect between the two.


Post a Comment

Popular posts from this blog

Custom validation messages for HTML5 Input elements using the constraint validation API

HTML5 has introduced several input types such as EMAIL, URL, RANGE, SEARCH, DATE, TIME, etc,. Most of the modern browsers have implemented them and are ready to be used in a HTML document. Another exciting feature introduced in HTML5 is the form validation. Instead of writing JavaScript to validate users input, browsers can now validate it and show an appropriate message if the validation fails. The validation message is shown in line with the field for which the validation has failed. The default error message is shown when the validation fails. In this post I'll explain how these error messages can be changed.

De-obfuscating javascript code in Chrome Developer Tools

I had blogged about JavaScript debugging with Chrome Developer Tools some time back, wherein I have explained how these developer tools can help in debugging javascript code. Today Google Chrome 12 was released and my Chrome browser was updated to this version. As with every release, there have been some improvements made on performance, usability etc,. One feature that stood out for me is the ability to De-obfuscate the javascript code.

What is Minification?

Minification is the process of removing unnecessary characters such as white spaces, comments, new lines from the source code. These otherwise would be added to make the code more readable. Minifying the source code helps in reducing the file size and thereby reducing the time taken to download the file. This is the reason why most of the popular javascript libraries such as jQuery are minified. A minified jQuery file is of 31 KB in size where as an uncompressed one is about 229 KB. Unfortunately, debugging minified javascript file…

Adding beforeRender and afterRender functions to a Backbone View

I was working on a Backbone application that updated the DOM when a response was received from the server. In a Backbone View, the initialize method would perform some operations and then call the render method to update the view. This worked fine, however there was scenario where in I wanted to perform some tasks before and after rendering the view. This can be considered as firing an event before and after the function had completed its execution. I found a very simple way to do this with Underscore's wrap method.