BoxLang 🚀 A New JVM Dynamic Language Learn More...
A comprehensive ColdFusion RESTful services compatibility module for BoxLang that provides compatibility for the CFML REST specification including routing, serialization, and OpenAPI documentation generation.
This module provides ColdFusion-compatible REST services for BoxLang web applications, supporting:
restInitApplication, restSetResponse, restDeleteApplication
httpMethod,
produces, consumes, restPath, restArgSource
box install bx-compat-rest
// 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 /bxrest/public/RestController.bx?method=processAPIRequest&requestPath=/api/hello
GET /bxrest/public/RestController.bx?method=processAPIRequest&requestPath=/api/hello/John
The central registry that:
The front controller that:
docs() methodParses ColdFusion REST annotations:
rest / restPath on componentshttpMethod on functionsproduces / consumes for content negotiationrestArgSource / restArgName for parameter bindingHandles request processing:
Provides serialization/deserialization:
rest="true"
: Marks component as REST-enabledrestPath="path"
: Base path for all functions in the componenthttpMethod="GET|POST|PUT|DELETE|..."
: HTTP method(s) the function handlesrestPath="path"
: Subpath for the function (supports {param} placeholders)produces="mime/type"
: Response content types (comma-separated)consumes="mime/type"
: Request content types (comma-separated)returnFormat="json|xml|plain"
: Default return formatrestArgSource="Path|Query|Form|Header|Cookie|Body"
: Where to extract parameter valuerestArgName="name"
: Name of the parameter in the sourcerequired="true|false"
: Whether the parameter is requireddefault="value"
: Default value if not providedRegisters 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
Throw CF-compatible errors:
remote function getUser(required numeric id restArgSource="Path") {
if (!userExists(id)) {
throw(
type="REST.NotFound",
message="User not found",
errorCode="404"
);
}
return getUser By(id);
}
The module automatically returns appropriate HTTP status codes:
The module includes comprehensive tests covering:
Run tests:
gradle test
Add the following servlet pass predicate to your
server.json file ( changing the prefix to your configured
prefix ):
path-prefix('/rest/') -> rewrite('/bxmodules/bxrest/public/RestController.bx?method=processAPIRequest&requestPath=%{RELATIVE_PATH}')
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:
See CONTRIBUTING.md for guidelines.
Apache License 2.0 - See LICENSE file for details.
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