1.SE24建立处理类
2.实现接口 IF_HTTP_EXTENSION
3.实现方法IF_HTTP_EXTENSION~HANDLE_REQUEST
代码语言:javascript复制METHOD if_http_extension~handle_request.
DATA : lr_json_ser TYPE REF TO cl_trex_json_serializer,
lr_json_des TYPE REF TO cl_trex_json_deserializer.
DATA : lv_json_string TYPE string.
DATA : lv_request TYPE string.
DATA : BEGIN OF lw_request,
werks TYPE werks_d,
END OF lw_request.
DATA lt_request LIKE TABLE OF lw_request.
CLEAR lt_request.
"获取传入JSON
lv_json_string = server->request->get_cdata( ).
"#JSON->ABAP内表
CALL METHOD /ui2/cl_json=>deserialize
EXPORTING
json = lv_json_string
CHANGING
data = lt_request.
"#返回JSON数据格式
CALL METHOD server->response->if_http_entity~set_content_type
EXPORTING
content_type = 'applicaion/json'.
"#获取数据
SELECT
matnr,
werks
INTO TABLE @DATA(lt_response)
FROM marc
UP TO 100 ROWS
FOR ALL ENTRIES IN @lt_request
WHERE werks = @lt_request-werks.
CLEAR lv_json_string.
"#返回内表数据ABAP转JSON
CREATE OBJECT lr_json_ser
EXPORTING
data = lt_response.
CALL METHOD lr_json_ser->serialize( ).
CALL METHOD lr_json_ser->get_data
RECEIVING
rval = lv_json_string.
"#返回JSON数据
server->response->set_cdata(
EXPORTING
data = lv_json_string ).
ENDMETHOD.