实践感受一下:
SE24新建类
实现接口IF_HTTP_EXTENSION
双击,实例化这个方法。
1 METHOD if_http_extension~handle_request. 2 DATA: BEGIN OF ls_data, 3 matnr TYPE matnr, 4 maktx TYPE maktx, 5 END OF ls_data. 6 7 DATA: lt_data LIKE TABLE OF ls_data. 8 9 DATA: lt_fields TYPE tihttpnvp. 10 FIELD-SYMBOLS: <fs_field> LIKE LINE OF lt_fields. 11 12 DATA: lv_method TYPE string. "获取GET/POST方式 13 DATA: lv_json TYPE string, "返回参数 14 lv_json1 TYPE string, "获取传回值数据 15 lv_json2 TYPE string. "获取传回值数据 16 17 18 * 获取JSON抬头数据 19 server->request->get_header_fields( 20 CHANGING 21 fields = lt_fields 22 ). 23 lv_method = server->request->get_header_field( name = ‘~request_method‘ )."获取GET/POST方式 24 CALL METHOD server->response->if_http_entity~set_content_type 25 EXPORTING 26 content_type = ‘application/json‘. 27 28 CASE lv_method. 29 WHEN ‘GET‘. 30 * 获取数据 31 SELECT matnr maktx INTO CORRESPONDING FIELDS OF TABLE lt_data 32 FROM z_v_atps23 33 UP TO 5 ROWS. 34 35 * 内表转换JSON 36 CALL METHOD /ui2/cl_json=>serialize 37 EXPORTING 38 data = lt_data 39 RECEIVING 40 r_json = lv_json. 41 42 * 将行数据JSON返回给调用端 43 server->response->set_cdata( 44 EXPORTING 45 data = lv_json 46 ). 47 48 server->response->set_status( code = 200 reason = ‘OK‘ ). 49 WHEN ‘POST‘. 50 51 * 获取JSON行数据 52 lv_json1 = server->request->if_http_entity~get_cdata( ). 53 * 解析json至内表方法1 54 /ui2/cl_json=>deserialize( EXPORTING json = lv_json1 55 CHANGING data = ls_data ). 56 IF NOT lv_json1 IS INITIAL. 57 * 获取数据 58 SELECT matnr maktx INTO CORRESPONDING FIELDS OF TABLE lt_data 59 FROM z_v_atps23 60 UP TO 10 ROWS 61 WHERE matnr = ls_data-matnr. 62 * 内表转换json 63 CALL METHOD /ui2/cl_json=>serialize 64 EXPORTING 65 data = lt_data 66 RECEIVING 67 r_json = lv_json. 68 * 将行数据JSON返回给调用端 69 server->response->set_cdata( 70 EXPORTING 71 data = lv_json 72 ). 73 ENDIF. 74 75 LOOP AT lt_fields INTO DATA(ls_fields). 76 CASE ls_fields-name. 77 WHEN ‘lt_data‘. 78 lv_json2 = ls_fields-value. 79 ENDCASE. 80 ENDLOOP. 81 * 解析json至内表方法1 82 /ui2/cl_json=>deserialize( EXPORTING json = lv_json2 83 CHANGING data = lt_data ). 84 85 IF NOT lv_json2 IS INITIAL. 86 * 将行数据JSON返回给调用端 87 server->response->set_cdata( 88 EXPORTING 89 data = ‘SoapUi传参已收到‘ 90 ). 91 ENDIF. 92 server->response->set_status( code = 200 reason = ‘OK‘ ). 93 94 ENDCASE. 95 96 ENDMETHOD.
TCODE:SICF 在路径/default_host/sap/bc/下新建并且激活服务,服务中选择上面创建的类。
END.
原文:https://www.cnblogs.com/mingdashu/p/15308044.html