ColdFusion 10: Accessing a REST service without specifying the Application name or Service mapping in URL
In my previous post, I’d explained how REST services can be created, published and accessed in ColdFusion 10. Andy, asked me “Is there any way to avoid having the /rest/restapp/ in the URL?”. In short Yes. ‘rest’ in the URL lets the ColdFusion Server know that the incoming request is for a REST service. As mentioned in my previous post, you can update the servlet mapping defined in web.xml located at cfusion_home\wwwroot\WEB-INF directory. But it can't be avoided. Coming to the ‘restapp’ in the URL which indicates the Application name or the Servlet mapping. It can be removed from the URL by placing the CFCs in a default directory. While registering a REST service in the ColdFusion Administrator, there is an option to set the service as a default service, meaning all the CFCs placed in the directory would not require the Application name or Service mapping to be provided in the URL.
Here’s the REST Services page in ColdFusion Administrator:
After providing the directory containing CFCs to be published as REST services, you can set the same as default application. The CFCs found in this directory will not require a mapping to be specified in the URL. One thing to note here is that, there can be only one such directory for the Server.
In the Administrator say you have provided a path to the directory containing a CFC as ‘C:\work\ColdFusion\cf_main\cfusion\wwwroot\restapp\default’ and the CFC contains a function that is to be exposed as a REST service:
Once this CFC has been registered with the Administrator it can be accessed using the URL: http://localhost:8500/rest/customerService/Sagar
Notice the URL now. It has the ‘customerService’ right after ‘rest’. 'customerService' is the restpath defined in the above CFC. There is no Application name or the Service mapping present in the URL.
Here’s the REST Services page in ColdFusion Administrator:
After providing the directory containing CFCs to be published as REST services, you can set the same as default application. The CFCs found in this directory will not require a mapping to be specified in the URL. One thing to note here is that, there can be only one such directory for the Server.
In the Administrator say you have provided a path to the directory containing a CFC as ‘C:\work\ColdFusion\cf_main\cfusion\wwwroot\restapp\default’ and the CFC contains a function that is to be exposed as a REST service:
<cfcomponent rest="true"
restpath="/customerService">
<cffunction name="getRequestHandler"
access="remote"
returntype="String"
httpmethod="GET"
produces="text/html"
restpath="{name}">
<cfargument name="name"
type="string"
restargsource="path"/>
<cfreturn "<h1>Hello " & name & "</h1>">
</cffunction>
</cfcomponent>
Once this CFC has been registered with the Administrator it can be accessed using the URL: http://localhost:8500/rest/customerService/Sagar
Notice the URL now. It has the ‘customerService’ right after ‘rest’. 'customerService' is the restpath defined in the above CFC. There is no Application name or the Service mapping present in the URL.
It would have been great if there was a way to define the service mapping at the application level. So, I would do:
ReplyDeletethis.restServicePath = "/api/";
and then just call http://[domain]/api/test
@db76faef2a1f4da09dfa3f56b28d3d87 What if there is a directory under the webroot with the name 'api'. How will the server decide which request to execute i.e http://domain/api/test/index.cfm or the REST service.
ReplyDeleteThe reason why rest is introduced in web.xml file is that the Servlet to execute a REST request will take a different route when compared to other cfm requests.
What will happen now if there is a directory under the webroot with the name 'api'? My guess is it will be ignored. I would think the same can be done at application level instead of Servlet level.
ReplyDeleteAlthough I like the idea, I think ignoring one over the other would not be the right decision. In a case there are many such Applications and each of these have a restServicePath that conflicts with a directory name then it wouldn't be right.
ReplyDeleteI wonder how that would work in a shared hosting environment.
I agree ignoring one over the other is not the right decision. But, my point is that's probably what CF will do now. ie. if I have a folder called 'rest' in the webroot, it will probably be ignored because the servlet is routing the request as a rest call.
ReplyDeleteDoing routing at the application level makes more sense as you can specify where your rest CFCs are (just like ORM cfcs) and then access them based on the path.
As you already mentioned, the current way it is setup, will not allow anyone in shared environment to have a url of their choice.
As mentioned in the post, the server owner\administrator always has an option of changing 'rest' to anything else by updating web.xml file. If there is a directory with name 'rest' then that would be ignored, but that's the only exception.
ReplyDeleteHi Sagar, can you give us an idea/sample how we can securing the new REST services with authentification? Like popular REST API with api-key or username and password.
ReplyDelete@roger there are various ways in which you can pass data to a REST service, please read my post on 'Understanding REST parameters' -
ReplyDeletehttp://www.sagarganatra.com/2012/02/coldfusion-10-understanding-rest.html . Let me know if this answers your question.