ODATA服务生成服务,通常是给外部系统使用的,通过HTTP的,GET ,POST,GET 等方式来使用此ODATA服务,前几天就想起一个问题,就是在SAP如何调用另外的系统或者本系统提供的ODATA服务呢,我之前想的办法是,是使用HTTP的GET,POST方法来读取,并解析,但前几天发现一个方法,文本写学习CDS的教程,但文中刚好用到了写一个ABAP程序来测试CDS生成的ODATA,所以记录一下。
1.首先是生成一个CDS视图,
CDS的使用方法,可以在本站查询到
1 2 3 4 5 6 7 8 9 10 11 |
@EndUserText.label: 'Jerry cube view' @Analytics.dataCategory: #CUBE @VDM.viewType: #COMPOSITE @AccessControl.authorizationCheck:#CHECK @AbapCatalog.sqlViewName: 'zprdcube' define view Z_C_Prod_Cube as select from zprd_query{ key zprd_query.prod_id, zprd_query.prod_text, @DefaultAggregation: #MAX zprd_query.quantity } |
2.生成一个ODATA。
1 2 3 4 5 6 7 8 9 10 11 12 |
@EndUserText.label: 'Jerry query verification' @VDM.viewType: #CONSUMPTION @Analytics.query: true @AccessControl.authorizationCheck:#NOT_ALLOWED @AbapCatalog.sqlViewName: 'zprdquery' @OData.publish: true define view Z_C_Product as select from Z_C_Prod_Cube { key Z_C_Prod_Cube.prod_id, Z_C_Prod_Cube.prod_text, @DefaultAggregation: #MAX Z_C_Prod_Cube.quantity } |
在第6行中。使用@OData.publish:true,因此在激活此查询视图时会自动生成一个新的OData服务。此ODATA服务还要使用事务码/IWFND/MAINT_SERVICE激活。
3.调用ODATA的ABAP代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
REPORT zcds_get_query_view_data. DATA(lo_tool) = NEW cl_nat_generic_dpc( ). DATA(lo_context) = NEW /iwbep/cl_mgw_context( ). DATA: l_r_rs_gw_columns TYPE REF TO cl_abap_tabledescr, l_t_rs_gw_columns TYPE REF TO data, lo_request TYPE REF TO /iwbep/cl_mgw_request, lo_detail TYPE REF TO /iwbep/if_mgw_core_srv_runtime=>ty_s_mgw_request_context, ls_detail TYPE /iwbep/if_mgw_core_srv_runtime=>ty_s_mgw_request_context, lt_header TYPE tihttpnvp, lt_filter TYPE /iwbep/t_mgw_select_option, lt_order TYPE /iwbep/t_mgw_sorting_order, ls_page TYPE /iwbep/s_mgw_paging, ls_header TYPE LINE OF tihttpnvp. FIELD-SYMBOLS: <l_t_rs_gw> TYPE table. lo_context->/iwbep/if_mgw_context~set_parameter( iv_name = /iwbep/if_mgw_context=>gc_param_isn iv_value = 'Z_C_PRODUCT_CDS' ). lo_context->/iwbep/if_mgw_context~set_parameter( iv_name = /iwbep/if_mgw_context=>gc_param_isv iv_value = '0001' ). lo_tool->/iwbep/if_mgw_core_srv_runtime~set_context( lo_context ). CREATE DATA lo_detail. lo_request = NEW /iwbep/cl_mgw_request( ir_request_details = lo_detail it_headers = lt_header ). DATA(lo_rt) = NEW cl_eq_bics_gw_rt( i_query = '2Czprdquery' i_servicetype_oq = abap_true ). lo_rt->get_designtime( IMPORTING e_t_column_description = DATA(l_t_query_struc) ) . l_r_rs_gw_columns = cl_eq_bics_gw_dt=>build_rs_structure( l_t_query_struc ). CREATE DATA l_t_rs_gw_columns TYPE HANDLE l_r_rs_gw_columns. ASSIGN l_t_rs_gw_columns->* TO <l_t_rs_gw>. ls_detail-technical_request-service_name = 'Z_C_PRODUCT_CDS'. ls_detail-technical_request-service_version = '0001'. ls_detail-technical_request-source_entity_type = ls_detail-technical_request-target_entity_type = 'Z_C_PRODUCTType'. ls_detail-technical_request-source_entity_set = ls_detail-technical_request-target_entity_set = 'Z_C_PRODUCTTypeCollection'. ls_header-name = 'dummy'. APPEND ls_header TO ls_detail-technical_request-request_header. CALL METHOD lo_tool->/iwbep/if_mgw_core_srv_runtime~read_entityset( EXPORTING iv_entity_name = 'Z_C_PRODUCTType' iv_source_name = 'Z_C_PRODUCTType' is_paging = ls_page it_order = lt_order it_filter_select_options = lt_filter is_request_details = ls_detail CHANGING cr_entityset = l_t_rs_gw_columns ct_headers = lt_header ). ASSIGN l_t_rs_gw_columns->* TO <l_t_rs_gw>. WRITE: 'lines of data: ', lines( <l_t_rs_gw> ). |
运行测试结果。