T-CODE:SICF
路径:/default_host/sap/bc/rest 右键创建新的子元素
登录数据可配、处理器清单为新建类(后续附代码)
示例新建类:ZCL_FI4_GET_XXX
1 METHOD if_http_extension~handle_request. 2 DATA: lv_req_json TYPE string, 3 lv_res_json TYPE string. 4 DATA:ls_zfi4_s_get_costcenter_in TYPE zfi4_s_get_costcenter_in, 5 ls_zfi4_s_get_costcenter_out TYPE zfi4_s_get_costcenter_out. 6 7 DATA: lt_fields TYPE tihttpnvp, 8 lt_uri_para TYPE tihttpnvp, 9 ls_uri_para TYPE ihttpnvp. 10 DATA: path TYPE string, 11 path_info TYPE string, 12 params TYPE string. 13 14 * 调用方式(GET/POST/PUT/DELETE) 15 DATA(lv_method) = server->request->get_header_field( '~request_method' ). 16 DATA(lv_content_type) = server->request->get_header_field( 'Content-Type' ). 17 18 * 解析请求,判断执行方法 19 path = server->request->get_header_field( '~request_uri' ). 20 SPLIT path AT '?' INTO path_info params. 21 22 "Get escaped URI 23 DATA(lv_escaped_uri) = server->request->get_header_field( '~request_uri' ). 24 "Decoding URI 25 DATA(lv_unescaped_uri) = cl_http_utility=>unescape_url( lv_escaped_uri ). 26 "Get host 27 DATA(lv_host) = server->request->get_header_field( 'host' ). 28 "Get Communication Protocol 29 DATA(lv_protocol) = server->request->get_header_field( '~uri_scheme_expanded' ). 30 TRANSLATE lv_protocol TO LOWER CASE. 31 "Concatenate URL 32 DATA(lv_url) = |{ lv_protocol }://{ lv_host }{ lv_unescaped_uri }|. 33 34 35 CASE lv_method. 36 WHEN 'GET'. 37 * Header fields 38 server->request->get_header_fields( CHANGING fields = lt_fields[] ). 39 * 获取URL 的 parameter 参数 和 value 40 CALL METHOD server->request->get_form_fields( CHANGING fields = lt_uri_para[] ). 41 42 SELECT SINGLE * FROM XXXX INTO @DATA(ls_list) WHERE if_id = 'ID号'. 43 IF ls_list-if_active = ''. 44 ls_zfi4_s_get_costcenter_out = VALUE #( result = 'false' code = 1 msg = 'ID号未激活,请在配置表XXXX激活此接口!'). 45 ELSE. 46 "1.JSON转为ABAP内表 47 CLEAR: ls_zfi4_s_get_costcenter_in. 48 LOOP AT lt_uri_para INTO ls_uri_para. 49 CASE ls_uri_para-name. 50 WHEN 'bukrs'. 51 ls_zfi4_s_get_costcenter_in-bukrs = ls_uri_para-value. 52 ENDCASE. 53 ENDLOOP. 54 55 CALL FUNCTION 'ZFI4_GET_COSTCENTER' 56 EXPORTING 57 input = ls_zfi4_s_get_costcenter_in 58 IMPORTING 59 output = ls_zfi4_s_get_costcenter_out. 60 ENDIF. 61 "3.ABAP内表转为JSON 62 lv_res_json = /ui2/cl_json=>serialize( data = ls_zfi4_s_get_costcenter_out compress = abap_true pretty_name = /ui2/cl_json=>pretty_mode-camel_case ). 63 WHEN 'POST'. 64 WHEN 'PUT'. 65 WHEN 'DELETE'. 66 WHEN OTHERS. 67 ENDCASE. 68 DATA(lv_status) = SWITCH #( ls_zfi4_s_get_costcenter_out-result WHEN 'true' THEN 'S' ELSE 'E' ). 69 * 判断接口是否需要记录调用日志 70 IF ls_list-if_log_active = 'X'." AND lv_status = 'E'."接口需要记录调用日志 71 *->Create Log Start 72 TRY. 73 cl_system_uuid=>if_system_uuid_static~create_uuid_x16( RECEIVING uuid = DATA(lv_guid) ). 74 CATCH cx_uuid_error INTO DATA(lo_err). 75 ENDTRY. 76 77 DATA(lo_toolkit) = NEW zcl_toolkit( ). 78 lo_toolkit->create_if_log( EXPORTING iv_guid = lv_guid iv_ifid = 'ZFII0002' iv_data = lv_req_json iv_step = 'REQ' iv_method = lv_method iv_url = CONV #( lv_url ) ). 79 lo_toolkit->create_if_log( EXPORTING iv_guid = lv_guid iv_ifid = 'ZFII0002' iv_data = lv_res_json iv_step = 'RSP' iv_status = lv_status ). 80 ENDIF. 81 *->Create Log End 82 * 将JSON返回给调用端 83 server->response->set_content_type( 'application/json' ). 84 server->response->set_cdata( lv_res_json ). 85 ENDMETHOD.View Code
ZFI4_GET_COSTCENTER代码
1 FUNCTION zfi4_get_costcenter. 2 *"---------------------------------------------------------------------- 3 *"*"本地接口: 4 *" IMPORTING 5 *" VALUE(INPUT) TYPE ZFI4_S_GET_COSTCENTER_IN 6 *" EXPORTING 7 *" VALUE(OUTPUT) TYPE ZFI4_S_GET_COSTCENTER_OUT 8 *"---------------------------------------------------------------------- 9 DATA:lv_line TYPE i, 10 lv_bukrs TYPE t001-bukrs, 11 et_tab TYPE zfi_t_get_kostl. 12 CLEAR lv_line. 13 SELECT SINGLE bukrs INTO lv_bukrs FROM csks WHERE bukrs = input-bukrs. 14 IF lv_bukrs IS INITIAL. 15 output-result = 'false'. 16 output-code = '0'. 17 output-msg = '暂无该公司代码下的成本中心记录,请核对公司代码'. 18 RETURN. 19 ELSE. 20 SELECT a~kostl b~ktext a~bukrs 21 INTO TABLE et_tab 22 FROM csks AS a 23 LEFT JOIN cskt AS b ON a~kokrs = b~kokrs AND a~kostl = b~kostl AND b~spras = 1 24 WHERE a~bukrs = input-bukrs 25 AND a~datbi >= sy-datum 26 AND a~datab <= sy-datum 27 AND a~kokrs = '1000'. 28 SORT et_tab. 29 DELETE ADJACENT DUPLICATES FROM et_tab COMPARING ALL FIELDS. 30 DESCRIBE TABLE et_tab LINES lv_line. 31 IF lv_line > 0. 32 output-result = 'true'. 33 output-code = '1'. 34 output-msg = '查询SAP成本中心成功!'. 35 output-data[] = et_tab[]. 36 ELSE. 37 output-result = 'false'. 38 output-code = '0'. 39 output-msg = '无数据!'. 40 ENDIF. 41 ENDIF. 42 ENDFUNCTION.View Code 标签:get,restful,uri,接口,lv,ls,TYPE,SAP,DATA From: https://www.cnblogs.com/coderfarmer/p/17040521.html