Attaching ABAP Processor Classes
Procedure
All resource URIs served by a REST service must be assigned via URI templates to ABAP processing classes. To assign classes for the resources provided by a REST service, you attach a fixed number of URI templates to ABAP classes. The attached ABAP classes implement the access to the resources based on the URI specified and the HTTP command used by a REST client. In the example, the resources are of type Car.
The simplest way to create an ABAP REST application is to implement an ABAP class derived from the library class CL_REST_HTTP_HANDLER. This class implements the interfaces IF_REST_APPLICATION and IF_HTTP_EXTENSION. The first interface is used by the REST framework to identify the processing classes of the REST application. The second interface is used to get used by the ICF framework to handle the HTTP REST request.
Perform the following steps:
-
Create an ABAP class (in this tutorial named CL_REST_TEST_APPLICATION) and set its super class to CL_REST_HTTP_HANDLER.
-
The class CL_REST_HTTP_HANDLER provides the interface method IF_REST_APPLICATION~GET_ROOT_HANDLER. This method attaches the resource URI templates to processing classes served by the REST application.
This is the coding of the method IF_REST_APPLICATION~GET_ROOT_HANDLER for attaching processor classes to URI templates in this tutorial.
method if_rest_application~get_root_handler.
data lo_handler type ref to cl_rest_router.
create object lo_handler.
lo_handler->attach( iv_template = '/Car/{ID:[1-9][0-9]*}' iv_handler_class = 'CL_REST_SAMPLE_CAR' ).
lo_handler->attach( iv_template = '/Cars' iv_handler_class = 'CL_REST_SAMPLE_CARS' ).
ro_root_handler = lo_handler.
endmethod.
-
The code creates an instance of the REST library router class CL_REST_ROUTER.
-
Several resource-URI templates get attached via method ATTACH to ABAP processing classes specified in parameter IV_HANDLER_CLASS:
-
CL_REST_SAMPLE_CAR
-
CL_REST_SAMPLE_CARS
-
-
The resource templates are specified via parameter IV_TEMPLATE. This parameter contains the resource identification template (using ABAP regular expression syntax), but not the complete call URI as used by a REST client. The root part of the URI is determined by the service-URI specified via the ICF, so the complete request URI is the concatenation of the service-URI and the resource part.
-
The routing instance with its attached processing classes is returned to make it available for the REST library during call processing.
The resource template /Car/{ID:[1-9][0-9]*} identifies a resource entity of type Car by an integer number greater than 1. With other words, at the URI position, where the regular expression is located, an integer value greater or equals to 1 is expected as path element.
The resource identifier is made available to the REST processing class by the specification of a formal parameter, which is named ID in the example. The actual called method depends on the HTTP method used during the client request and is handled in details in subsequent sections of this tutorial. The currently supported HTTP methods are: GET, POST, PUT, DELETE, HEAD, OPTIONS.
If there are multiple matches in the resource template list, then the resulting set gets sorted according to the specification Java API for RESTful Web Services (see JAX-RS Version 1.1: http://download.oracle.com/otndocs/jcp/jaxrs-1.1-mrel-eval-oth-JSpec/) and the first match in the sorted list is picked.