Reference

Detailed object reference for the RESTService object.

RESTService Object

Attribute

Description

addEndpoint(opts, fn)

Define an endpoint in your REST Service, a service may be built with 1 or more distinct endpoints.

setRequestProcessor(fn)

Define a request processor, every invocation of the REST Service will invoke this method before execution, it is useful if you want to filter something form the request before the endpoint method execution and you have the same logic at all endpoints.

setResponseProcessor(fn)

Define a response processor, every invocation of the REST Service will invoke this method after execution, it is useful if you want to setup the response headers after the endpoint has been processed.

setResponseBodyProcessor(fn)

Define the body processor, whenever you return data from your endpoint the data will be filtered using the implementation provided. The default implementation will transform data returned in JSON format.

setPolicy(key)

This function sets a policy to be checked in every invocation. If the credential used to access the API or the user logged does not have permission in that policy a forbidden access error will be returned.

addEndpoint([opts], fn)

Define an endpoint in your REST Service, your service may be built with 1 or more distinct endpoints.

opts

This argument should contain the attributes required to setup your endpoint.

name the name of the Endpoint, used by the JavaScript controller to automatically call this endpoint.
method the HTTP method from which this endpoint will be called. (GET, POST, PUT or DELETE).
path The endpoint path, can receive url-friendly parameters (which will be available form the param object).
allowAnon Overrides the allowAnon option of the RESTService for this endpoint only.
policy Creates a permission checkpoint validation, if the authorized user has no permission to execute that policy a 403 Forbidden error will be returned.

fn

You may use Declarative Syntax or the Imperative Syntax (classic) passing a Function or an Array as the fn parameter.

Function.

Using the imperative syntax when your REST Service is called, you receive all the parameters in a single object.

RESTService.addEndpoint({ 
    "name": "defaultGet", 
    "method": "GET", 
    "path": "/{id}" 
},
function doProccess(params) {
    // params will contain the query parameters, 
    // the payload object (for POST ou PUT methods)
    // and also the path parameters (:id) for example
});

Array

The declarative syntax is a bit more dynamic and you can declare which parameters you wish to receive:

$context Transfer Object used for sending session information from the View layer to the Business Delegate layer. Used for determining the Database Execution Context;
$path path info (obtained from HttpServletRequest.getPathInfo());
$headers headers from requisition;
$user holds SessionInfo for the logged user;
$data All parameters from requisition;
$params all info contained in $data and $path;
$query All params passed through the URL;

Besides that, it is possible to access path parameters by name simply by using its name in the array.
All parameters will be accessible by the function in the declared order, hence the last argument is the function itself:

RESTService.addEndpoint({ 
    "name": "defaultPut", 
    "method": "PUT", 
    "path": "/{id}" 
},
[ "id", "$data", function doProccess(id, data) {
    // id will contain the path parameter: id
    // data is the payload object(for POST ou PUT methods)
}]);

There's a slight difference between both and it's up to you to decide what suits you better.

setRequestProcessor(fn)

This function will receive a "request" object with all headers and data provided by the request.

setResponseProcessor(fn)

This function will receive a "response" object where you can define response headers or status code of your returned data.

See the "Using CORS" section for an example of usage.

setResponseBodyProcessor(fn)

This function will receive the endpoint definition, the response object and the data returned from the endpoint. The method must serialize the data and write into the response object, also including headers and status codes.

📘

JSON

The default implementation of this method is what transforms your data returned into the serialized JSON.

setPolicy(key)

This function sets a policy to be checked in every invocation. If the credential used to access the API or the user logged does not have permission in that policy a forbidden access error will be returned.

Updated less than a minute ago

Reference


Detailed object reference for the RESTService object.

Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.