REST Services

Railo 4.0 now also includes the capability to provide REST services.

REST services are a much easier way to manage the exchange of data and the creation of API’s for external consumption. In Railo we make it easy to use existing components and convert them to REST services.

All you have to do is create a mapping to your components in the Archives and Resources - Rest section of the administrator:

And then create your component adding the restpath and rest attributes:

component restpath="/hello" rest="true"{
remote any function helloRailo() httpmethod="GET" {
return "Hello, Railo!";
}
remote any function helloURL(string a restargsource="Path") httpmethod="GET" restpath="{a}" {
return "Hello, #ARGUMENTS.a#!";
}
remote any function helloFORM(string a restargsource="Form") httpmethod="POST" {
return "Hello, #ARGUMENTS.a#!";
}
}

Once you have done that, you can simply call the component via:

http://localhost:8888/rest/demo/hello/

Of course, if you also need to you can access the other methods as:

http url="http://localhost:8888/rest/demo/hello/" method="GET";
dump(cfhttp);
http url="http://localhost:8888/rest/demo/hello/railo-url" method="GET";
dump(cfhttp);
http url="http://localhost:8888/rest/demo/hello/" method="POST" {
httpparam name="a" value="railo-form" type="formField";
}
dump(cfhttp);

Binary Objects over REST

Railo based REST services also allow you to serve binary files, for example you can serve up images such as:

remote any function testPNG() httpmethod="GET" restpath="testPNG" produces="image/png" {
return ImageRead(request.currentPath&"test.png");
}

But Railo doesn’t just handle images, it can also (and we thing this is pretty cool!) is to return serialized Java objects, which returns a binary stream back. For example:

remote any function testJava() httpmethod="GET" restpath="testJava" produces="application/java" {
return [{a:1}];
}

You can then consume this as a native Java object again from Railo:

<cfhttp url="#baseURL#/rest/restPC/metatest/testJava" method="get" resolveurl="no">
</cfhttp>
<cfset res=evaluateJava(cfhttp.FileContent)>

This is the fastest way to deliver content from Railo server to Railo server, even faster than sending information over JSON, WDDX or XML