FUNCTION ZRFC_FI_SEND_PAYPLAN2BPM.
*"----------------------------------------------------------------------
*"*"本地接口:
*" IMPORTING
*" VALUE(INPUT) TYPE ZSRFC_FI_SEND_PAYBPM_IN
*" EXPORTING
*" VALUE(OUTPUT) TYPE ZRFC_RETURN
*"----------------------------------------------------------------------
DATA: LO_HTTP_CLIENT TYPE REF TO IF_HTTP_CLIENT.
DATA: LO_WRITER TYPE REF TO CL_SXML_STRING_WRITER.
DATA:LV_ERR_TEXT TYPE STRING,
LV_JSON_OUT TYPE XSTRING,
LV_REQUEST TYPE STRING,
LV_URL TYPE AGR_URL,
LV_OUTPUT_LENGTH TYPE I,
LV_URL_STR TYPE STRING,
LT_BINARY_TAB TYPE STANDARD TABLE OF SDOKCNTBIN,
RESULT TYPE STRING,
LV_SERVER_NAME TYPE ZE_SERVER_NAME,
LS_ITEM TYPE ZPDT_SEND_PAYBPM_REQ_DATA,
LT_ITEM TYPE TABLE OF ZPDT_SEND_PAYBPM_REQ_DATA,
LV_LEN TYPE I.
LV_SERVER_NAME = 'PAYPLAN'.
SELECT SINGLE HOST
INTO LV_URL
FROM /THINKWAY/TBUS0A
WHERE EXCHANGE_SYSTEM = 'BPM'
AND INSTANCE = 'PAYPLAN'.
IF SY-SUBRC <> 0.
OUTPUT-MSGTY = 'E'.
OUTPUT-MSGTX = '未找到目标地址'.
ELSE.
LV_URL_STR = LV_URL.
CONDENSE LV_URL_STR.
CALL METHOD CL_HTTP_CLIENT=>CREATE_BY_URL
EXPORTING
URL = LV_URL_STR
IMPORTING
CLIENT = LO_HTTP_CLIENT
EXCEPTIONS
ARGUMENT_NOT_FOUND = 1
PLUGIN_NOT_ACTIVE = 2
INTERNAL_ERROR = 3
OTHERS = 4.
*content_type
LO_HTTP_CLIENT->REQUEST->SET_CONTENT_TYPE( CONTENT_TYPE = 'application/json; charset=utf-8' ).
LO_HTTP_CLIENT->REQUEST->SET_METHOD( IF_HTTP_REQUEST=>CO_REQUEST_METHOD_POST ).
CLEAR LT_ITEM[].
LOOP AT INPUT-ITEM INTO DATA(LS_INPUT).
LS_INPUT-LIFNR = |{ LS_INPUT-LIFNR ALPHA = OUT }|.
LS_INPUT-BELNR = |{ LS_INPUT-BELNR ALPHA = OUT }|.
LS_INPUT-HKONT = |{ LS_INPUT-HKONT ALPHA = OUT }|.
MOVE-CORRESPONDING LS_INPUT TO LS_ITEM.
CONDENSE LS_ITEM-ZZDMBTR NO-GAPS.
CONDENSE LS_ITEM-WRBTR NO-GAPS.
LS_ITEM-ZFBDT = LS_ITEM-BUDAT.
APPEND LS_ITEM TO LT_ITEM.
ENDLOOP.
LO_WRITER = CL_SXML_STRING_WRITER=>CREATE( TYPE = IF_SXML=>CO_XT_JSON ).
CALL TRANSFORMATION ID SOURCE DATA = LT_ITEM[] RESULT XML LO_WRITER.
LV_JSON_OUT = LO_WRITER->GET_OUTPUT( ).
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
BUFFER = LV_JSON_OUT
IMPORTING
OUTPUT_LENGTH = LV_OUTPUT_LENGTH
TABLES
BINARY_TAB = LT_BINARY_TAB.
CALL FUNCTION 'SCMS_BINARY_TO_STRING'
EXPORTING
INPUT_LENGTH = LV_OUTPUT_LENGTH
IMPORTING
TEXT_BUFFER = LV_REQUEST
OUTPUT_LENGTH = LV_OUTPUT_LENGTH
TABLES
BINARY_TAB = LT_BINARY_TAB.
LV_LEN = STRLEN( LV_REQUEST ).
REPLACE ALL OCCURRENCES OF REGEX '"CONTROLLER":\[\],' IN LV_REQUEST WITH ''.
*- 通过PO抓取BPM的数据
LV_LEN = STRLEN( LV_REQUEST ).
CALL METHOD LO_HTTP_CLIENT->REQUEST->SET_HEADER_FIELD
EXPORTING
NAME = '~request_method'
VALUE = 'POST'.
CALL METHOD LO_HTTP_CLIENT->REQUEST->SET_CDATA
EXPORTING
DATA = LV_REQUEST
OFFSET = 0
LENGTH = LV_LEN.
CALL METHOD LO_HTTP_CLIENT->SEND
EXCEPTIONS
HTTP_COMMUNICATION_FAILURE = 1
HTTP_INVALID_STATE = 2
HTTP_PROCESSING_FAILED = 3
HTTP_INVALID_TIMEOUT = 4
OTHERS = 5.
"接收
CALL METHOD LO_HTTP_CLIENT->RECEIVE
EXCEPTIONS
HTTP_COMMUNICATION_FAILURE = 1
HTTP_INVALID_STATE = 2
HTTP_PROCESSING_FAILED = 3.
RESULT = LO_HTTP_CLIENT->RESPONSE->GET_CDATA( ).
LO_HTTP_CLIENT->CLOSE( ).
DATA: LJSONNODE TYPE REF TO ZCL_MDP_JSON_NODE.
DATA:L_VALUEOBJ TYPE REF TO ZCL_MDP_JSON_NODE.
LJSONNODE = ZCL_MDP_JSON_NODE=>DESERIALIZE( JSON = RESULT ).
FREE LO_HTTP_CLIENT.
L_VALUEOBJ = LJSONNODE->OBJECT_GET_CHILD_NODE( KEY = 'isSuccess' ).
IF L_VALUEOBJ->VALUE = 'true'.
OUTPUT-MSGTY = 'S'.
OUTPUT-MSGTX = LJSONNODE->OBJECT_GET_CHILD_NODE( KEY = 'errorMsg' )->VALUE.
ELSE.
OUTPUT-MSGTY = 'E'.
OUTPUT-MSGTX = LJSONNODE->OBJECT_GET_CHILD_NODE( KEY = 'errorMsg' )->VALUE.
ENDIF.
ENDIF.
ENDFUNCTION.
以上 ABAP转json后会产生
"DATA":{"LT_ITEM":[{"CONTROLLER":[],"BUKRS":"1040","BUTXT":"XXXXX机械科技有限公司","LIFNR":"2000","NAME1":"XXXXX科技有限公司","BELNR":"100027622","BUZEI":"001","BUDAT":"20240930","BLDAT":"20240930","WRBTR":"49041.11","ZTERM":"C999","ZFBDT":"20240930","ZFAEL":"31","ZZ....
的数据,而实际的 {"CONTROLLER":[],是不需要的
所以要用到 REPLACE ALL OCCURRENCES OF REGEX '"CONTROLLER":\[\],' IN LV_REQUEST WITH ''.
本来replace就可以了,因为有多个 所以 用到 REPLACE ALL OCCURRENCES OF REGEX
注意 '"CONTROLLER":\[\],' 因为特殊符号 [] 在,所以要用到转义一下
参考数据
写于20241010 台州