1、创建 SEGW对象
2、创建Entity Type
要把Media 标识打上
3、 激活对象然后到DPC Class的扩展对象里面重定义
/IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_STREAM
/IWBEP/IF_MGW_APPL_SRV_RUNTIME~CREATE_STREAM
/IWBEP/IF_MGW_APPL_SRV_RUNTIME~UPDATE_STREAM
METHOD /iwbep/if_mgw_appl_srv_runtime~get_stream.
TRY.
DATA: ls_stream TYPE ty_s_media_resource,
ls_file TYPE zmmp_matl_file,
lv_matnr TYPE matnr.
READ TABLE it_key_tab ASSIGNING FIELD-SYMBOL(<fs_key>) INDEX 1.
DATA: lv_filename TYPE char30.
lv_matnr = <fs_key>-value.
SELECT SINGLE * FROM zmmp_matl_file INTO ls_file WHERE matnr = lv_matnr .
IF ls_file IS NOT INITIAL.
ls_stream-value = ls_file-value.
ls_stream-mime_type = ls_file-mimetype.
copy_data_to_ref( EXPORTING is_data = ls_stream
CHANGING cr_data = er_stream ).
ENDIF.
CATCH /iwbep/cx_mgw_busi_exception.
CATCH /iwbep/cx_mgw_tech_exception.
ENDTRY.
ENDMETHOD.
METHOD /iwbep/if_mgw_appl_srv_runtime~create_stream.
TRY.
DATA: ls_file TYPE zmmp_matl_file.
DATA: lt_hdr_params TYPE tihttpnvp,
ls_hdr_params TYPE ihttpnvp.
DATA: lo_facade TYPE REF TO /iwbep/if_mgw_dp_int_facade.
REFRESH: lt_hdr_params.
lo_facade ?= /iwbep/if_mgw_conv_srv_runtime~get_dp_facade( ).
lt_hdr_params = lo_facade->get_request_header( ).
READ TABLE lt_hdr_params INTO ls_hdr_params WITH KEY name = 'material'. "header message
IF sy-subrc = 0.
ls_file-matnr = ls_hdr_params-value.
ENDIF.
READ TABLE lt_hdr_params INTO ls_hdr_params WITH KEY name = 'zmatserial'. "header message
IF sy-subrc = 0.
ls_file-zmat_serial = ls_hdr_params-value.
ENDIF.
IF ls_file-matnr IS INITIAL OR ls_file-zmat_serial IS INITIAL.
ls_file-value = is_media_resource-value.
ls_file-mimetype = is_media_resource-mime_type.
ls_file-crt_dat = sy-datum.
ls_file-crt_tim = sy-uzeit.
MODIFY zmmp_matl_file FROM ls_file.
copy_data_to_ref(
EXPORTING
is_data = ls_file
CHANGING
cr_data = er_entity ).
ELSE.
DATA ls_msg TYPE scx_t100key.
ls_msg-attr1 = '上传失败'.
RAISE EXCEPTION TYPE /iwbep/cx_mgw_tech_exception
EXPORTING
textid = ls_msg.
ENDIF.
CATCH /iwbep/cx_mgw_busi_exception.
CATCH /iwbep/cx_mgw_tech_exception.
ENDTRY.
ENDMETHOD.
METHOD /iwbep/if_mgw_appl_srv_runtime~update_stream.
TRY.
DATA: ls_file TYPE zmmp_matl_file.
DATA: lt_hdr_params TYPE tihttpnvp,
ls_hdr_params TYPE ihttpnvp.
DATA: lo_facade TYPE REF TO /iwbep/if_mgw_dp_int_facade.
REFRESH: lt_hdr_params.
lo_facade ?= /iwbep/if_mgw_conv_srv_runtime~get_dp_facade( ).
lt_hdr_params = lo_facade->get_request_header( ).
READ TABLE it_key_tab ASSIGNING FIELD-SYMBOL(<fs_key>) WITH KEY name = 'Matnr'.
IF sy-subrc = 0.
ls_file-matnr = <fs_key>-value.
ENDIF.
READ TABLE it_key_tab ASSIGNING <fs_key> WITH KEY name = 'ZmatSerial'.
IF sy-subrc = 0.
ls_file-zmat_serial = <fs_key>-value.
ENDIF.
ls_file-value = is_media_resource-value.
ls_file-mimetype = is_media_resource-mime_type.
ls_file-crt_dat = sy-datum.
ls_file-crt_tim = sy-uzeit.
MODIFY zmmp_matl_file FROM ls_file.
CATCH /iwbep/cx_mgw_busi_exception.
CATCH /iwbep/cx_mgw_tech_exception.
ENDTRY.
ENDMETHOD.
4、部署ODATA服务
5、测试服务
使用POST则会调用Create_Stream,而使用Update则会使用Update_Stream
注意再使用Create_Stream的时候,可能会报错,
Invalid or no mapping to system data types found
这个需要定义一下MPC_EXT中的DEFINE
METHOD define.
super->define( ).
DATA: lo_entity TYPE REF TO /iwbep/if_mgw_odata_entity_typ,
lo_property TYPE REF TO /iwbep/if_mgw_odata_property.
lo_entity = model->get_entity_type( iv_entity_name = 'MaterialFileStru' ).
IF lo_entity IS BOUND.
lo_property = lo_entity->get_property( iv_property_name = 'Mimetype').
lo_property->set_as_content_type( ).
ENDIF.
ENDMETHOD.
还可以使用Update_Stream