其他分享
首页 > 其他分享> > SAP REST 接口 JSON方式

SAP REST 接口 JSON方式

作者:互联网

1、新建类 ZCL_FI4_GET_COSTCENTER,接口IF_HTTP_EXTENSION

 

 2、方法代码

 

 

 1     DATA: lv_req_json TYPE string,
 2           lv_res_json TYPE string.
 3     DATA:ls_zfi4_s_get_costcenter_in  TYPE zfi4_s_get_costcenter_in,
 4          ls_zfi4_s_get_costcenter_out TYPE zfi4_s_get_costcenter_out.
 5 
 6     DATA: lt_fields   TYPE tihttpnvp,
 7           lt_uri_para TYPE tihttpnvp,
 8           ls_uri_para TYPE ihttpnvp.
 9     DATA: path      TYPE string,
10           path_info TYPE string,
11           params    TYPE string.
12 
13 * 调用方式(GET/POST/PUT/DELETE)
14     DATA(lv_method) = server->request->get_header_field( '~request_method' ).
15     DATA(lv_content_type) = server->request->get_header_field( 'Content-Type' ).
16 
17 * 解析请求,判断执行方法
18     path = server->request->get_header_field( '~request_uri' ).
19     SPLIT path AT '?' INTO path_info params.
20 
21     "Get escaped URI
22     DATA(lv_escaped_uri) = server->request->get_header_field( '~request_uri' ).
23     "Decoding URI
24     DATA(lv_unescaped_uri) = cl_http_utility=>unescape_url( lv_escaped_uri ).
25     "Get host
26     DATA(lv_host) = server->request->get_header_field( 'host' ).
27     "Get Communication Protocol
28     DATA(lv_protocol) = server->request->get_header_field( '~uri_scheme_expanded' ).
29     TRANSLATE lv_protocol TO LOWER CASE.
30     "Concatenate URL
31     DATA(lv_url) = |{ lv_protocol }://{ lv_host }{ lv_unescaped_uri }|.
32 
33 
34     CASE lv_method.
35       WHEN 'GET'.
36 *         Header fields
37         server->request->get_header_fields( CHANGING fields = lt_fields[] ).
38 *         获取URL 的 parameter 参数 和 value
39         CALL METHOD server->request->get_form_fields( CHANGING fields = lt_uri_para[] ).
40 
41         SELECT SINGLE * FROM ztcom_if_list INTO @DATA(ls_list) WHERE if_id = 'ZFII0002'.
42         IF ls_list-if_active = ''.
43           ls_zfi4_s_get_costcenter_out = VALUE #( result = 'false' code = 1 msg = 'PDA成本中心查询未激活ZFII0002,请在配置表ztcom_if_list激活此接口!').
44         ELSE.
45           "1.JSON转为ABAP内表
46           CLEAR: ls_zfi4_s_get_costcenter_in.
47           LOOP AT lt_uri_para INTO ls_uri_para.
48             CASE ls_uri_para-name.
49               WHEN 'bukrs'.
50                 ls_zfi4_s_get_costcenter_in-bukrs = ls_uri_para-value.
51             ENDCASE.
52           ENDLOOP.
53 
54           CALL FUNCTION 'ZFI4_GET_COSTCENTER'
55             EXPORTING
56               input  = ls_zfi4_s_get_costcenter_in
57             IMPORTING
58               output = ls_zfi4_s_get_costcenter_out.
59         ENDIF.
60         "3.ABAP内表转为JSON
61         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 ).
62       WHEN 'POST'.
63       WHEN 'PUT'.
64       WHEN 'DELETE'.
65       WHEN OTHERS.
66     ENDCASE.
67     DATA(lv_status) = SWITCH #( ls_zfi4_s_get_costcenter_out-result WHEN 'true' THEN 'S' ELSE 'E' ).
68 *   判断接口是否需要记录调用日志
69     IF ls_list-if_log_active = 'X'." AND lv_status = 'E'."接口需要记录调用日志
70 *->Create Log Start
71       TRY.
72           cl_system_uuid=>if_system_uuid_static~create_uuid_x16( RECEIVING uuid = DATA(lv_guid) ).
73         CATCH cx_uuid_error INTO DATA(lo_err).
74       ENDTRY.
75 
76       DATA(lo_toolkit) = NEW zcl_toolkit( ).
77       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 ) ).
78       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 ).
79     ENDIF.
80 *->Create Log End
81 * 将JSON返回给调用端
82     server->response->set_content_type( 'application/json' ).
83     server->response->set_cdata( lv_res_json ).
View Code

注意:上述调用的FUCTION和RFC远程可调用函数

接口参数结构如下图:

 

 

 

 

3、SICF配置接口  /default_host/sap/bc/rest/

 

4、假如开发环境200,测试环境300,想连300测试,可在登录数据页签配置环境账号

 

 

道友们发现有错误可随时指正

标签:zfi4,get,uri,REST,lv,JSON,ls,SAP,DATA
来源: https://www.cnblogs.com/coderfarmer/p/16501666.html