Principle:APIJSON APIJSON Demo Custom Remote Functions
| Knowledge Sources | |
|---|---|
| Domains | API_Design, Extensibility, Server_Side_Logic |
| Last Updated | 2026-10-14 00:00 GMT |
Overview
Extensibility mechanism that allows client-side JSON requests to invoke server-side Java methods or multi-language scripts, enabling dynamic computation and business logic without predefined endpoints.
Description
Custom Remote Functions let APIJSON clients call server-side logic directly from JSON requests using a key() or key-() syntax. The server resolves the function name to a registered Java method (via the Method database table) or a script executor (JavaScript, Lua), executes it with the provided arguments, and inserts the result into the JSON response.
This solves:
- Fixed endpoint limitation: Traditional REST APIs require a new endpoint for each business operation. Remote functions allow extending the API without new routes.
- Client-side computation gaps: Some values (current user ID, computed lists, validation) cannot be expressed in pure SQL — remote functions fill this gap.
- Multi-language scripting: Business logic may be written in JavaScript (GraalVM/Nashorn) or Lua, enabling non-Java developers to contribute logic.
The function invocation flow:
- Client sends JSON with
"key()":"functionName(args)" - Framework looks up function name in the Method database table
- If Java method: invoke via reflection with
curObjand arguments - If script: delegate to registered ScriptExecutor (GraalJS, Nashorn, Lua)
- Result is inserted at the
keyposition in the response JSON
Usage
Use this principle when:
- Clients need server-computed values (current user, ID lists, cross-table validation) in their queries.
- Business logic should be extensible without redeploying the server (via database-registered scripts).
- You want to support multiple scripting languages for server-side extensions.
Theoretical Basis
Function Invocation Pipeline:
Client Request:
{
"User": {
"id@": "getCurrentUserId()", // Remote function call
"Comment[]": {
"Comment": {
"userId@": "User/id" // Reference to function result
}
}
}
}
Server Resolution:
1. Parser encounters "getCurrentUserId()" as function call
2. Look up in Method table: package, method, arguments
3. Invoke:
- Java: DemoFunctionParser.getCurrentUserId(curObj) → Long
- Script: GraalJavaScriptExecutor.execute(parser, curObj, name, args) → Object
4. Insert result: "id" = 82001
5. Continue parsing with resolved value
Function types:
- Java functions: Methods on DemoFunctionParser annotated with
@NotNull JSONObject curObjparameter. Registered via Method database table. - Script functions: Loaded via
ScriptExecutor.load(name, script), executed with_metacontext (version, tag, args) and optionalextParambinding. - Validation functions: Return void, throw exception on failure (e.g.,
verifyIdList,verifyURLList).