SAP 發(fā)布HTTP接口
1.SE24創(chuàng)建類
在接口中填寫 IF_HTTP_EXTENSION 繼承該類并激活
實(shí)現(xiàn)繼承的HANDLE_REQUEST方法:IF_HTTP_EXTENSION~HANDLE_REQUEST
METHOD if_http_extension~handle_request.
DATA: lt_fields TYPE tihttpnvp,
lv_data TYPE string,
lv_html TYPE string.
DATA:BEGIN OF it,
modelcode TYPE zppt0002-zzxh,
carcode TYPE zppt0002-zcjnum,
carname TYPE zppt0002-zzmc,
cencode TYPE zppt0002-zzkbm,
modelname TYPE zppt0002-zzmc,
confnum TYPE zppt0002-matnr,
carcolor TYPE zppt0002-zzys,
motornum TYPE zppt0002-zdjnum,
END OF it.
DATA:item LIKE TABLE OF it.
DATA: start_date TYPE erdat,
end_date TYPE erdat,
str1 TYPE string,
str2 TYPE string,
str3 TYPE string,
key TYPE string,
value TYPE string.
DATA:lin TYPE sy-tabix.
DATA: wa LIKE LINE OF lt_fields.
FIELD-SYMBOLS: <fs_field> LIKE LINE OF lt_fields.
*" get HEADER fields
server->request->get_header_fields(
CHANGING
fields = lt_fields " Header fields
).
lv_data = server->request->if_http_entity~get_cdata( ).
" Read the fields table and look for name "~query_string" -- this will contain the URL query
READ TABLE lt_fields INTO wa WITH KEY name = '~query_string'.
IF sy-subrc = 0.
SPLIT wa-value AT '&' INTO str1 str2 str3.
IF str2 IS INITIAL OR str3 IS INITIAL .
start_date = sy-datum.
end_date = sy-datum.
ELSE.
SPLIT str2 AT '=' INTO key value.
start_date = value.
CLEAR:key,value.
SPLIT str3 AT '=' INTO key value.
end_date = value.
ENDIF.
ENDIF.
* 獲取數(shù)據(jù)
SELECT
zppt0002~zzxh AS modelcode
zppt0002~zcjnum AS carcode
zppt0002~zzmc AS carname
zppt0002~zzkbm AS cencode
zppt0002~matnr AS confnum
zppt0002~zzys AS carcolor
zppt0002~zdjnum AS motornum
INTO CORRESPONDING FIELDS OF TABLE item
FROM zppt0002
INNER JOIN zcxsz ON zcxsz~matnr = zppt0002~matnr
WHERE zppt0002~ZODAT >= start_date AND zppt0002~ZODAT <= end_date AND zppt0002~ZSTATUS = 'Z05'.
IF sy-subrc EQ 0.
DESCRIBE TABLE item LINES lin.
* 拼接json字符串
LOOP AT item INTO it.
IF sy-tabix = 1.
lv_html = '['.
ENDIF.
IF lin = sy-tabix.
CONCATENATE lv_html '{'
'"modelCode":"' it-modelcode '",'
'"carCode":"' it-carcode '",'
'"carName":"' it-carname '",'
'"cenCode":"' it-cencode '",'
'"modelName":"' it-carname '",'
'"confNum":"' it-confnum '",'
'"carColor":"' it-carcolor '",'
'"motorNum":"' it-motornum '"'
'}]'
INTO lv_html.
ELSE.
CONCATENATE lv_html '{'
'"modelCode":"' it-modelcode '",'
'"carCode":"' it-carcode '",'
'"carName":"' it-carname '",'
'"cenCode":"' it-cencode '",'
'"modelName":"' it-carname '",'
'"confNum":"' it-confnum '",'
'"carColor":"' it-carcolor '",'
'"motorNum":"' it-motornum '"'
'},'
INTO lv_html. "SEPARATED BY space.
ENDIF.
ENDLOOP.
* 返回響應(yīng)
server->response->set_cdata(
EXPORTING
data = lv_html " Character data
* offset = 0 " Offset into character data
* length = -1 " Length of character data
).
ENDIF.
* 設(shè)置返回響應(yīng)格式為json
CALL METHOD server->response->if_http_entity~set_content_type
EXPORTING
content_type = 'application/json'.
ENDMETHOD.
2.定義服務(wù)棒动,SICF創(chuàng)建服務(wù)
設(shè)置服務(wù)的賬號(hào)及密碼
在處理器清單填寫上面我們實(shí)現(xiàn)的類
保存激活,測(cè)試服務(wù)
因我本地未配置SAP域名映射所以測(cè)試失敗
改為IP地址后測(cè)試通過
將接口地址開放到外部
通過nginx反向代理肝劲,進(jìn)行防火墻映射迁客。
到此結(jié)束整個(gè)接口開發(fā)郭宝。
后記
此處數(shù)據(jù)轉(zhuǎn)json,前期我采用轉(zhuǎn)換類cl_trex_json_serializer如下掷漱,但是其轉(zhuǎn)換后的json格式并不是標(biāo)準(zhǔn)的json格式粘室,key值缺少雙引號(hào)。比較簡單我直接進(jìn)行了拼接
DATA lc_json TYPE REF TO cl_trex_json_serializer. "json格式轉(zhuǎn)換
CREATE OBJECT lc_json.
CALL METHOD lc_json->serialize_int
EXPORTING
data = lt_vldata
RECEIVING
r_json = l_json_data.
TRANSLATE l_json_data TO LOWER CASE.
CONCATENATE l_url l_json_data INTO l_url.
后期尋找后卜范,解決方法是將該類復(fù)制修改其RECURSE方法組合形式就可以采用此種方式
借鑒資料
SAP發(fā)布REST/HTTP接口
SAP調(diào)用RestfulApi接口POST數(shù)據(jù)到外部系統(tǒng)
SAP的JSON沒有雙引號(hào)問題