一、SE24创建类:Z_HCX_HTTP
1、创建类:
2、切换到接口(interface)页签,输入IF_HTTP_EXTENSION ,回车。切换到方法(method)页签,双击IF_HTTP_EXTENSION~HANDLE_REQUEST进入代码编辑界面。
文章来源地址https://www.toymoban.com/news/detail-505725.html
3、在IF_HTTP_EXTENSION~HANDLE_REQUEST方法中编写代码:(注:文末附另一种写法)
METHOD if_http_extension~handle_request.
DATA: BEGIN OF ls_data,
matnr TYPE matnr,
maktx TYPE maktx,
END OF ls_data.
DATA: lt_data LIKE TABLE OF ls_data.
DATA: lt_fields TYPE tihttpnvp.
FIELD-SYMBOLS: <fs_field> LIKE LINE OF lt_fields.
DATA: lv_method TYPE string. "获取GET/POST方式
DATA: lv_json TYPE string, "返回参数
lv_json1 TYPE string, "获取传回值数据
lv_json2 TYPE string. "获取传回值数据
* 获取JSON抬头数据
server->request->get_header_fields(
CHANGING
fields = lt_fields
).
lv_method = server->request->get_header_field( name = '~request_method' )."获取GET/POST方式
CALL METHOD server->response->if_http_entity~set_content_type
EXPORTING
content_type = 'application/json'.
CASE lv_method.
WHEN 'GET'.
* 获取数据
SELECT matnr maktx INTO CORRESPONDING FIELDS OF TABLE lt_data
FROM makt
UP TO 20 ROWS.
* 内表转换JSON
CALL METHOD /ui2/cl_json=>serialize
EXPORTING
data = lt_data
RECEIVING
r_json = lv_json.
* 将行数据JSON返回给调用端
server->response->set_cdata(
EXPORTING
data = lv_json
).
server->response->set_status( code = 200 reason = 'OK' ).
WHEN 'POST'.
* 获取JSON行数据
lv_json1 = server->request->if_http_entity~get_cdata( ).
* 解析json至内表方法1
/ui2/cl_json=>deserialize( EXPORTING json = lv_json1
CHANGING data = ls_data ).
IF NOT lv_json1 IS INITIAL.
* 获取数据
SELECT matnr maktx INTO CORRESPONDING FIELDS OF TABLE lt_data
FROM makt
UP TO 20 ROWS
where matnr = ls_data-matnr.
* 内表转换json
CALL METHOD /ui2/cl_json=>serialize
EXPORTING
data = lt_data
RECEIVING
r_json = lv_json.
* 将行数据JSON返回给调用端
server->response->set_cdata(
EXPORTING
data = lv_json
).
ENDIF.
LOOP AT lt_fields INTO DATA(ls_fields).
CASE ls_fields-name.
WHEN 'lt_data'.
lv_json2 = ls_fields-value.
ENDCASE.
ENDLOOP.
* 解析json至内表方法1
/ui2/cl_json=>deserialize( EXPORTING json = lv_json2
CHANGING data = lt_data ).
IF NOT lv_json2 IS INITIAL.
* 将行数据JSON返回给调用端
server->response->set_cdata(
EXPORTING
data = 'SoapUi传参已收到'
).
ENDIF.
server->response->set_status( code = 200 reason = 'OK' ).
ENDCASE.
ENDMETHOD.
二、使用事务码 SICF 在 default_host/sap/bc 节点下面创建子节点,用 Z_HCX_HTTP 类并激活方法:
1、创建子元素:
之后点击保存。
2、在default_host/sap中找到刚刚创建的子元素:Z_HCX_HTTP激活服务:
3、获取URL调用地址:
直接打开默认是GET请求:
在Postman 中测试post请求:
在SoapUI 中测试post请求:
文末附:上文步骤3提到的另一种写法。
1、手动再依次添加GET和POST方法,再依次选中,点击参数,添加参数SERVER - 类型Importing - 键入方法Type Ref To - 关联类型IF_HTTP_SERVER
2、双击IF_HTTP_EXTENSION~HANDLE_REQUEST编写代码
method IF_HTTP_EXTENSION~HANDLE_REQUEST.
DATA(LV_METHOD) = SERVER->REQUEST->GET_METHOD( )."查看调用方法GET OR POST
CASE LV_METHOD.
WHEN 'GET'.
ME->GET( SERVER ).
WHEN 'POST'.
ME->POST( SERVER ).
WHEN OTHERS.
"Sets current HTTP status code
SERVER->RESPONSE->SET_STATUS( CODE = 405 REASON = '请求方法不支持,请联系管理员!' ).
ENDCASE.
endmethod.
3、根据业务需求情况,选择GET或POST编写代码,以GET为例:
method GET.
TYPES:BEGIN OF TY_TAB,
AUFNR TYPE AFPO-AUFNR, "生产订单号
* WERKS TYPE MARC-WERKS, "工厂
* MATNR TYPE AFPO-MATNR, "物料号
END OF TY_TAB.
DATA:GT_TAB TYPE TABLE OF TY_TAB,
GS_TAB TYPE TY_TAB.
TYPES:BEGIN OF TY_OUT,
MATNR TYPE AFPO-MATNR, "物料号
LGORT TYPE AFPO-LGORT, "库存地点
PSMNG TYPE AFPO-PSMNG, "总数量
END OF TY_OUT.
DATA:GT_OUT TYPE TABLE OF TY_OUT,
GS_OUT TYPE TY_OUT.
DATA:
BEGIN OF LS_REQUEST,
ACTION TYPE STRING, " 可以用来区分具体对应的处理类
DATA TYPE TABLE OF TY_TAB, " 表结构
END OF LS_REQUEST,
LV_JSON TYPE STRING,
LV_CONTENT_TYPE TYPE STRING,
LV_JSON_OUT TYPE STRING,
LT_RETURN TYPE STANDARD TABLE OF BAPIRETURN,
LS_RETURN TYPE BAPIRETURN.
" DELIVERS THE HTTP BODY OF THIS ENTITY AS CHARACTER DATA
LV_JSON = SERVER->REQUEST->GET_CDATA( ).
IF LV_JSON IS INITIAL.
SERVER->RESPONSE->SET_STATUS( CODE = 404 REASON = '未解析到BODY参数,请检查' ).
RETURN.
ENDIF.
" 将JSON转换成内表
/UI2/CL_JSON=>DESERIALIZE( EXPORTING JSON = LV_JSON CHANGING DATA = LS_REQUEST ).
CASE LS_REQUEST-ACTION.
WHEN 'ZGETPRINT'.
WHEN OTHERS.
SERVER->RESPONSE->SET_STATUS( CODE = 404 REASON = '未解析到对应ACTION,请检查' ).
RETURN.
ENDCASE.
IF LS_REQUEST-DATA IS NOT INITIAL.
GT_TAB = LS_REQUEST-DATA.
ENDIF.
LOOP AT GT_TAB INTO GS_TAB.
"补全生产订单号的前导0.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
INPUT = GS_TAB-AUFNR
IMPORTING
OUTPUT = GS_TAB-AUFNR.
MODIFY GT_TAB FROM GS_TAB.
ENDLOOP.
* 获取打印数据
SELECT MATNR LGORT PSMNG INTO CORRESPONDING FIELDS OF TABLE GT_OUT
FROM AFPO FOR ALL ENTRIES IN GT_TAB
WHERE AUFNR = GT_TAB-AUFNR.
IF GT_OUT IS INITIAL.
SERVER->RESPONSE->SET_CDATA( /UI2/CL_JSON=>SERIALIZE( DATA = 'CODE:500, SAP API SELECT ERROR' ) ).
RETURN.
ENDIF.
LOOP AT GT_OUT INTO GS_OUT.
"去除物料号的前导0.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
EXPORTING
INPUT = GS_OUT-MATNR
IMPORTING
OUTPUT = GS_OUT-MATNR.
MODIFY GT_OUT FROM GS_OUT.
ENDLOOP.
* 内表转换JSON
CALL METHOD /UI2/CL_JSON=>SERIALIZE
EXPORTING
DATA = GT_OUT
RECEIVING
R_JSON = LV_JSON_OUT.
* 将行数据JSON返回给调用端
SERVER->RESPONSE->SET_CDATA(
EXPORTING
DATA = LV_JSON_OUT ).
endmethod.
4、请求示例
{
"ACTION": "ZGETPRINT",
"DATA": [
{
"AUFNR":"1000003"
}
]
}
5、响应示例
A、[{"MATNR":"11000042","LGORT":"4071","PSMNG":1620.000}]
B、"CODE:500, SAP API SELECT ERROR" 注:未获取到数据文章来源:https://www.toymoban.com/news/detail-505725.html
到了这里,关于SAP ABAP 使用SICF发布HTTP API接口的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!