系列文章索引
SAP 如何提供 RESTful Web 服务?
SAP 如何提供 RESTful Web 服务(2) - ABAP 与 JSON
SAP 如何提供 RESTful Web 服务(3) - Rest 路径处理
解决 Rest 路径问题
本篇解决路径问题。
SICF 设置的是一个固定的路径,但 Rest 风格的 API 要求使用不同的路径和不同的 HTTP 请求来实现不同的操作。比如有一个学生的数据需要 CRUD:
- 列出所有学生 (GET):
/base-url/students
, - 创建新的学生 (POST):
/base-rul/students/create
- 修改学生 (PUT):
/base-url/students/4
- 删除学生 (DELETE):
/base-url/students/6
根据 SAP Netweaver 的版本,可以使用 SAP 提供的标准 Rest 框架:cl_rest_http_handler
和 cl_rest_resource
来实现。如果版本比较早, SAP 中没有这两个类库的话,可以选择 DJ Adams 所写的 dispatcher 和 resource 类。DJ Adams的 博客 和 Github 上的 源码 请自行参考。
出于演示目的,本篇在处理方法 request_handler
方法中简单地实现根据浏览器提交的路径,进行不同的处理。
首先在 SAP 中创建一个 zemployee
表:
然后使用事务码 SE24 新建一个类:ZCL_EMP_HANDLER
,实现 IF_HTTP_EXTENSION
接口。在 handle_request
方法中写下面的处理代码:
METHOD if_http_extension~handle_request.
DATA: lv_verb TYPE string,
path TYPE string,
path_info TYPE string,
params TYPE string.
DATA: lt_employee TYPE STANDARD TABLE OF zemployee,
ls_employee LIKE LINE OF lt_employee.
DATA: req_json TYPE string.
DATA: serializer TYPE REF TO zcl_trex_json_serializer,
deserializer TYPE REF TO zcl_trex_json_deserializer,
lv_json TYPE string.
DATA: result_tab TYPE match_result_tab ,
wa LIKE LINE OF result_tab.
lv_verb = server->request->get_header_field( '~request_method' ) .
path = server->request->get_header_field( '~request_uri' ).
SPLIT path AT '?' INTO path_info params.
* POST http Method
* list all employees
IF lv_verb = 'GET' AND path_info = '/zrest/employees'.
SELECT * FROM zemployee INTO TABLE lt_employee.
CREATE OBJECT serializer
EXPORTING DATA = lt_employee[].
CALL METHOD serializer->serialize( ).
lv_json = serializer->get_data( ).
server->response->set_status( code = 200 reason = 'Ok' ).
server->response->set_content_type( 'application/json' ).
server->response->set_cdata( data = lv_json ).
ENDIF.
* POST http method: create new employee
IF lv_verb = 'POST' AND path_info = '/zrest/employees/create'.
req_json = server->request->get_cdata( ).
CLEAR ls_employee.
CREATE OBJECT deserializer.
CALL METHOD deserializer->deserialize(
EXPORTING json = req_json
IMPORTING abap = ls_employee ).
MODIFY zemployee FROM ls_employee.
server->response->set_status( code = 201 reason = 'Employee created successfully.' ).
ENDIF.
* PUT http method: modify employee
IF lv_verb = 'PUT'.
REFRESH result_tab.
FIND ALL OCCURRENCES OF REGEX '/zrest/employees/\d+' IN path_info RESULTS result_tab.
IF NOT result_tab IS INITIAL.
req_json = server->request->get_cdata( ).
CLEAR ls_employee.
CREATE OBJECT deserializer.
CALL METHOD deserializer->deserialize(
EXPORTING json = req_json
IMPORTING abap = ls_employee ).
MODIFY zemployee FROM ls_employee.
server->response->set_status( code = 200 reason = 'Employee updated successfully.' ).
ENDIF.
ENDIF.
* DELETE http method: delete employee
IF lv_verb = 'DELETE'.
REFRESH result_tab.
FIND ALL OCCURRENCES OF REGEX '/zrest/employees/\d+' IN path_info RESULTS result_tab.
IF NOT result_tab IS INITIAL.
DATA: f1 TYPE string,
f2 TYPE string,
f3 TYPE string,
emp_id TYPE string.
CLEAR wa.
READ TABLE result_tab INTO wa INDEX 1.
path_info = path_info+wa-offset(wa-length) .
SPLIT path_info AT '/' INTO f1 f2 f3 emp_id.
DELETE FROM zemployee WHERE empid = emp_id.
server->response->set_status( code = 204 reason = 'Employee deleted successfully.' ).
ENDIF.
ENDIF.
ENDMETHOD.
代码比较直观,无需解释。因仅作为示例用途,未考虑严谨性,比如未考虑修改 employee
表时检查 empid
字段是否存在等。
完成之后通过 SICF 在 default_host 下面新建 zrest
子节点,提供对外的 web 服务。
Http 请求测试
选择一个自己喜欢的测试工具,比如 Postman ,对涉及的四种 HTTP 请求进行测试。以下是测试示例:
数据库表刚开始有两条记录:
测试 GET 请求
测试 POST 请求
测试 PUT 请求
测试 DELETE 请求