METHOD IF_HTTP_EXTENSION~HANDLE_REQUEST. *--------------------------Variables-----------------------------------* CONSTANTS: LC_I TYPE C LENGTH 1 VALUE 'I', LC_EQ TYPE C LENGTH 2 VALUE 'EQ', LC_GET TYPE STRING VALUE 'GET', LC_PUT TYPE STRING VALUE 'PUT', LC_POST TYPE STRING VALUE 'POST', LC_PATCH TYPE STRING VALUE 'PATCH', LC_DELETE TYPE STRING VALUE 'DELETE', LC_METHOD TYPE STRING VALUE '~request_method', LC_RETUTYPE TYPE STRING VALUE 'application/json'. "应用数据结构与内表 DATA: BEGIN OF LS_DATA, CARRID TYPE SFLIGHT-CARRID, CONNID TYPE SFLIGHT-CONNID, FLDATE TYPE SFLIGHT-FLDATE, PRICE TYPE SFLIGHT-PRICE, CURRENCY TYPE SFLIGHT-CURRENCY, END OF LS_DATA, LT_DATA LIKE TABLE OF LS_DATA, LR_CARRID TYPE RANGE OF SFLIGHT-CARRID, LR_CONNID TYPE RANGE OF SFLIGHT-CONNID, LR_FLDATE TYPE RANGE OF SFLIGHT-FLDATE, LS_CARRID LIKE LINE OF LR_CARRID, LS_CONNID LIKE LINE OF LR_CONNID, LS_FLDATE LIKE LINE OF LR_FLDATE.
"JSON抬头结构 DATA: LT_FIELDS TYPE TIHTTPNVP.
"接口属性 DATA: LV_MESGTEXT TYPE STRING, LV_RETUCODE TYPE STRING, LV_METHOD TYPE STRING, "获取GET/POST等请求方式 LV_RETU_JSON TYPE STRING, "返回参数的JSON LV_GET_JSONS TYPE STRING, "获取单条的JSON LV_GET_JSONT TYPE STRING. "获取多条的JSON *----------------------------Logic-------------------------------------* "获取JSON抬头数据 SERVER->REQUEST->GET_HEADER_FIELDS( CHANGING FIELDS = LT_FIELDS ).
"获取请求方式 LV_METHOD = SERVER->REQUEST->GET_HEADER_FIELD( NAME = LC_METHOD ).
"获取JSON行数据 LV_GET_JSONS = SERVER->REQUEST->IF_HTTP_ENTITY~GET_CDATA( ). "可选,主要是为了JSON了可读性 REPLACE ALL OCCURRENCES OF REGEX '\s(?=([^"]*"[^"]*")*[^"]*$)' IN LV_GET_JSONS WITH ''. " 正则压缩json字符串空格
"将JSON转换为内表 IF LV_GET_JSONS IS NOT INITIAL. /UI2/CL_JSON=>DESERIALIZE( EXPORTING JSON = LV_GET_JSONS CHANGING DATA = LS_DATA ). ELSE. "获取多条数据的JSON LOOP AT LT_FIELDS INTO DATA(LS_FIELDS). TRANSLATE LS_FIELDS-NAME TO UPPER CASE. IF LS_FIELDS-NAME EQ 'LT_DATA'. LV_GET_JSONT = LS_FIELDS-VALUE. ENDIF. CLEAR: LS_FIELDS. ENDLOOP.
"判断请求方式 CASE LV_METHOD. WHEN LC_GET OR LC_POST. "获取筛选条件 LS_CARRID-SIGN = LC_I. LS_CARRID-OPTION = LC_EQ. MOVE-CORRESPONDING LS_CARRID TO LS_CONNID. MOVE-CORRESPONDING LS_CARRID TO LS_FLDATE.
IF LS_DATA IS NOT INITIAL. APPEND LS_DATA TO LT_DATA. ENDIF.
IF LT_DATA IS NOT INITIAL. LOOP AT LT_DATA INTO LS_DATA. IF LS_DATA-CARRID IS NOT INITIAL. LS_CARRID-LOW = LS_DATA-CARRID. APPEND LS_CARRID TO LR_CARRID. ENDIF.
IF LS_DATA-CONNID IS NOT INITIAL. LS_CONNID-LOW = LS_DATA-CONNID. APPEND LS_CONNID TO LR_CONNID. ENDIF.
IF LS_DATA-FLDATE IS NOT INITIAL. LS_FLDATE-LOW = LS_DATA-FLDATE. APPEND LS_FLDATE TO LR_FLDATE. ENDIF. CLEAR: LS_DATA. ENDLOOP. ENDIF.
SELECT CARRID CONNID FLDATE PRICE CURRENCY FROM YSFLIGHT_CYH INTO CORRESPONDING FIELDS OF TABLE LT_DATA WHERE CARRID IN LR_CARRID AND CONNID IN LR_CONNID AND FLDATE IN LR_FLDATE. IF SY-SUBRC NE 0. LV_RETUCODE = 'E'. LV_MESGTEXT = '数据未找到'. ELSE. LV_RETUCODE = 'S'. LV_MESGTEXT = '数据查询成功'. ENDIF. WHEN LC_PUT. WHEN LC_PATCH. WHEN LC_DELETE. WHEN OTHERS. ENDCASE.