RESTful Routing
RESTfule routing, binds to a specific path (or wildcard path) and MUST extend the synapticloop.nanohttpd.router.RestRoutable
abstract class.
Additionally, RESTful routes also allow mapping of URI paths to parameters which are automatically bound and passed through
Instantiation
All classes that extend RestRoutable
will be automatically instantiated through the reading and parsing of the routemaster.properties
file.
public RestRoutable(String routeContext, List<String> params) {
super(routeContext);
this.restParamNames = params;
}
Params
The params that are accepted by the RestRouter
are always mapped to the name in the configuration file %parameter_name%
. For example the mapped route in the SimpleRestServant
:
rest./simple/%mapped%/=synapticloop.nanohttpd.example.servant.SimpleRestServant
With the key mapped
- denoted by the %mapped%
token in the URL.
When the URL /simple/GET/
is invoked, then the SimpleRestServant
public Response serve(File rootDir, IHTTPSession httpSession)
is invoked which then goes through the parameters and maps them on key:value pairs. (e.g. mapped:GET
the mapped
key will return a value of KEY
).
Methods
This class invokes the method, depending on the HTTP verb that is requested.
switch(httpSession.getMethod()) {
case GET:
return(doGet(rootDir, httpSession, restParams, unmappedParams));
case POST:
return(doPost(rootDir, httpSession, restParams, unmappedParams));
case PUT:
return(doPut(rootDir, httpSession, restParams, unmappedParams));
case DELETE:
return(doDelete(rootDir, httpSession, restParams, unmappedParams));
case HEAD:
return(doHead(rootDir, httpSession, restParams, unmappedParams));
case TRACE:
return(doTrace(rootDir, httpSession, restParams, unmappedParams));
case PATCH:
return(doPatch(rootDir, httpSession, restParams, unmappedParams));
case CONNECT:
return(doConnect(rootDir, httpSession, restParams, unmappedParams));
case OPTIONS:
return(doOptions(rootDir, httpSession, restParams, unmappedParams));
default:
return(HttpUtils.methodNotAllowedResponse());
}
As an example the HTTP GET request will invoke the following method.
public Response doGet(File rootDir, IHTTPSession httpSession, Map<String, String^gt; restParams, String unmappedParams) {
return(HttpUtils.methodNotAllowedResponse());
}
Unless over-ridden, all methods will return the HttpUtils.methodNotAllowedResponse()
.