BoxLang 🚀 A New JVM Dynamic Language Learn More...
A comprehensive CFML RESTful services compatibility module for BoxLang that provides compatibility for the CFML REST specification including routing, serialization, and OpenAPI documentation generation.
This module provides CFML-compatible REST services for BoxLang web applications, supporting:
restInitApplication, restSetResponse, restDeleteApplication
httpMethod,
produces, consumes, restPath, restArgSource
web section of your server.json
"web":{
"servletPassPredicate":"regex( '^/(.+?\\.cf[cms])(/.*)?$' ) or regex( '^/(.+?\\.bx[sm])(/.*)?$' ) or path-prefix-nocase( /rest/ )",
...
}
server.json to allow the servlet to
pass REST routes to BoxLangPlace the following in a file ( for example
.rest-override.xml )
<web-app>
<servlet-mapping>
<servlet-name>BoxLangServlet</servlet-name>
<url-pattern>rest/*</url-pattern>
</servlet-mapping>
</web-app>
Then update your server.json to point to your override file.
"app" : {
"webXMLOverride" : "./.rest-override.xml",
...
}
.server.json scripts block:
"scripts" : {
"onServerInitialInstall" : "install bx-plus,bx-compat-rest"
}
The following configuration items are supported and can be configured
in the modules block of your boxlang.json or
.cfconfig.json file:
"bx-compat-rest":{
"settings":{
// The entry prefix to use for all rest applications
"entryPrefix" : "/rest/",
// A struct containing the service name and fully expanded route to the directory containing the REST classes
"applications":{
// Key-value pairs
"api":"/home/resty/my/rest/api/classes",
// Key with struct to specify default application
"resty" : {
"path" : "/home/resty/my/rest/defaultAPI",
"default" : true
}
}
}
}
// HelloService.bx
component rest="true" restPath="hello" {
remote string function sayHello() httpMethod="GET" produces="application/json" {
return "Hello, World!";
}
remote string function sayHelloTo(
required string name restArgSource="Path"
) httpMethod="GET" restPath="{name}" produces="application/json" {
return "Hello, #arguments.name#!";
}
}
In your Application.cfc:
component {
this.name = "MyRESTApp";
// Register REST application on startup
function onApplicationStart() {
restInitApplication(
expandPath("./services"), // Directory containing REST CFCs
"api" // Service mapping name
);
}
}
GET /rest/api/hello
GET /rest/api/hello/John
Registers a REST application directory.
restInitApplication(
rootPath, // Required: Directory containing REST CFCs
serviceName, // Optional: Service name/mapping
options // Optional: Struct with host, useHost, isDefault
)
Example:
restInitApplication(expandPath("./api"), "myapi", {
"host": "api.example.com",
"isDefault": true
});
Sets a custom HTTP response with status, content, and headers.
restSetResponse({
"status": 201,
"content": {"id": 123, "message": "Created"},
"headers": {"Location": "/api/resource/123"}
});
Removes a registered REST application.
restDeleteApplication("myapi");
Access auto-generated OpenAPI 3.0 documentation:
GET /bxrest/public/RestController.bx?method=docs
The documentation includes:
remote function getUser(
required numeric id restArgSource="Path"
) httpMethod="GET" restPath="users/{id}" {
return getUserById(arguments.id);
}
Access as: /api/users/123
remote function createUser(
required string name restArgSource="Form"
) httpMethod="POST" {
var newUser = saveUser(arguments.name);
restSetResponse({
"status": 201,
"content": newUser,
"headers": {"Location": "/api/users/" & newUser.id}
});
return newUser;
}
The module automatically handles content negotiation based on:
Accept header for responsesContent-Type header for requestsSupported formats:
application/json, text/json
application/xml, text/xml
application/x-www-form-urlencoded
multipart/form-data
text/plain
remote function getUser(required numeric id restArgSource="Path") {
if (!userExists(id)) {
restSetResponse(
{
"status" : 404,
"content" : "User not found"
}
);
return;
}
return getUser By(id);
}
The module automatically returns appropriate HTTP status codes:
This module implements the ColdFusion RESTful services specification as documented in: https://helpx.adobe.com/coldfusion/developing-applications/changes-in-coldfusion/restful-web-services-in-coldfusion.html
Supported features:
Ortus Solutions, Corp https://www.ortussolutions.com
All notable changes to this project will be documented in this file.
The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.
$
box install bx-compat-rest