OnRESTRequest has NOT been removed. Why do you say that it has?
In any case, what follows is a REST application I have just created on ColdFusion 2021. The files share the same directory.
/* Application.cfc */
component output="false"
{
this.name = "RestTestApp";
this.applicationTimeout = createTimespan(0,1,0,0);
this.sessionTimeout = createTimespan(0,0,20,0);
this.restsettings.skipCFCWithError = true;
public boolean function onRequestStart()
{
restInitApplication(getDirectoryFromPath(getCurrentTemplatePath()), this.name);
return true;
}
public void function onError(exception, eventName) {
// For debugging purposes
writedump(var=exception, label="Exception in OnError", format="html", output="#expandPath('RESTException.html')#");
}
public any function onRESTRequest(string cfcname, string method, struct args) {
var RESTResponse = invoke(arguments.cfcname, arguments.method , arguments.args);
// For debugging purposes
writedump(var=application, label="Application Scope dump within OnRESTRequest", format="html", output="#expandPath('OnRESTRequest_dump.html')#");
writedump(var=arguments, label="OnRESTRequest Arguments Scope", format="html", output="#expandPath('OnRESTRequest_dump.html')#");
return RESTResponse;
}
}
/* User.cfc */
/* CFC to simulate User entity. Accessors = true. Hence ColdFusion automatically generates getters and setters for each property */
component accessors="true" output="false"
{
property name="userid" type="numeric" ;
property name="username" type="string";
property name="firstname" type="string";
property name="lastname" type="string";
property name="email" type="string";
/* Simulate data for a user whose ID is 9876 */
public User function init() {
variables.userid=9876;
variables.username="jsmth";
variables.firstname="John";
variables.lastname="Smith";
variables.email="john.smith@fakenews.com";
return this;
}
}
/* RestSample.cfc */
component restpath="users" rest="true"
{
remote struct function getUser(numeric userid restargsource="Path") httpmethod="GET" restpath="{userid}"
{
var user = new User();
var response = structNew();
if(arguments.userid eq 9876)
{
response.userid = user.getUserId();
response.username = user.getUsername();
response.firstname = user.getFirstname();
response.lastname = user.getLastname();
response.email = user.getEmail();
} else {
throw(type="Restsample.UserNotFoundError", errorCode='404', detail='User not found');
}
return response;
}
}
<!--- getUserFromId.cfm: Get a user whose userID is 9876 --->
<cfhttp
url="http://localhost:8500/rest/RestTestApp/users/9876?format=json"
method="GET"
result="res">
</cfhttp>
<cfdump var = "#res#">