一、SE24创建类:Z_HCX_HTTP
1、创建类:
2、切换到接口(interface)页签,输入IF_HTTP_EXTENSION ,回车。切换到方法(method)页签,双击IF_HTTP_EXTENSION~HANDLE_REQUEST进入代码编辑界面。
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: LIKE LINE OF lt_fields. DATA: lv_method TYPE string. "获取GET/POST方式 DATA: lv_jsonTYPE 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 POSTCASE LV_METHOD.WHEN 'GET'.ME->GET( SERVER ).WHEN 'POST'.ME->POST( SERVER ).WHEN OTHERS. "Sets current HTTP status codeSERVER->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 DATALV_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'EXPORTINGINPUT= GS_TAB-AUFNRIMPORTINGOUTPUT = GS_TAB-AUFNR.MODIFY GT_TAB FROM GS_TAB.ENDLOOP.* 获取打印数据SELECT MATNR LGORT PSMNG INTO CORRESPONDING FIELDS OF TABLE GT_OUTFROM AFPO FOR ALL ENTRIES IN GT_TABWHERE 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'EXPORTINGINPUT= GS_OUT-MATNRIMPORTINGOUTPUT = GS_OUT-MATNR.MODIFY GT_OUT FROM GS_OUT.ENDLOOP.*内表转换JSONCALL METHOD /UI2/CL_JSON=>SERIALIZEEXPORTINGDATA = GT_OUTRECEIVINGR_JSON = LV_JSON_OUT.* 将行数据JSON返回给调用端SERVER->RESPONSE->SET_CDATA(EXPORTINGDATA = LV_JSON_OUT ).endmethod.
4、请求示例
{
“ACTION”:“ZGETPRINT”,
“DATA”:[
{
“AUFNR”:“1000003”
}
]
}
5、响应示例
A、[{“MATNR”:”11000042″,”LGORT”:”4071″,”PSMNG”:1620.000}]
B、“CODE:500,SAPAPISELECTERROR” 注:未获取到数据