SAP ABAP RESTFUL
学习内容:
创建一个http接口服务
-
实例一个HTTP框架接口(IF_HTTP_EXTENSION )
-
创建一个服务接口
路径:sicf—>default_host/sap/
保存激活,右键测试服务获取URL -
实例类中写业务逻辑
TYPES:BEGIN OF ty_body,
matnr TYPE matnr,
maktx TYPE maktx.
TYPES END OF ty_body.
DATA:lv_str TYPE string,
lv_charset TYPE string,
lv_xstr TYPE xstring,
lv_length TYPE i,
lv_content_type TYPE string,
lv_encoding TYPE abap_encoding,
lc_conv TYPE REF TO cl_abap_conv_in_ce,
lc_otconv TYPE REF TO cl_abap_conv_out_ce.
DATA:lv_dsjson TYPE REF TO zcl_json_document.
DATA:"ls_json TYPE data,
lt_fields TYPE tihttpnvp.
DATA:BEGIN OF ls_json,
werks TYPE werks_d,
detail TYPE TABLE OF ty_body.
DATA END OF ls_json.
****获取16进制数据流
lv_xstr = server->request->get_data( ).
****根据content-type信息获取字符集
lv_content_type = server->request->get_header_field( 'content-type' ).
lv_charset = zcl_devp_tools=>get_instance( )->get_charset( lv_content_type ).
****根据字符集查找SAP字符集codepage
lv_encoding = CONV abap_encoding( zcl_devp_tools=>get_instance( )->get_encoding( CONV #( lv_charset ) ) ).
****根据字符集解析数据流
lc_conv = cl_abap_conv_in_ce=>create( encoding = lv_encoding ).
lc_conv->convert( EXPORTING input = lv_xstr
IMPORTING data = lv_str ).
****将json数据转换到abap(也可使用标准类/ui2/cl_json)
lv_dsjson = zcl_json_document=>create_with_json( json = lv_str ).
lv_dsjson->get_data( IMPORTING data = ls_json ).
****得到abap数据后进行业务逻辑处理
..............................
..............................
..............................
****返回结果****
ls_json-detail = VALUE #( BASE ls_json-detail ( matnr = '11231231' maktx = '我是物料3?' ) ).
CLEAR lv_dsjson.
****abap数据转json
lv_dsjson = zcl_json_document=>create_with_data( data = ls_json ).
lv_str = lv_dsjson->get_json( ).
****根据字符集将json数据转16进制数据流
lc_otconv = cl_abap_conv_out_ce=>create( encoding = lv_encoding ).
lc_otconv->convert( EXPORTING data = lv_str
IMPORTING buffer = lv_xstr ).
****设置响应参数及响应信息
server->response->set_status( code = '200' reason = 'OK' ).
server->response->set_content_type( 'application/json' ).
server->response->set_data( data = lv_xstr ).