分类: Web开发
2015-06-30 19:07:06
T-code:SICF中建立一个服务来处理http请求并激活服务
路径: /default_host/sap/bc/
在登陆数据:设置用户密码
在处理器清单中输入一个处理类
T-code:se24定义处理类ZCL_REST_CALL, 需要继承IF_HTTP_EXTENSION的方法
在方法IF_HTTP_EXTENSION~HANDLE_REQUEST处理http请求
METHOD if_http_extension~handle_request.
DATA: lv_method TYPE string,
lv_content_type TYPE string,
lv_parameter TYPE string,
lv_result TYPE string,
lv_xdata TYPE xstring,
lv_xdata_o TYPE xstring,
lv_json TYPE string.
********************************************************************************
CLEAR: lv_method, lv_content_type, lv_parameter, lv_result .
lv_method = server->request->get_header_field( '~request_method' ). " 调用方式(POST/GET)
IF lv_method = 'GET'.
* http://crm-dev.lp.com:8001/sap/bc/ztest_rest1?sap-client=200&AA=1&BB=23
** 获得输入参数
lv_parameter = server->request->get_header_field( name = '~query_string' ).
** 接口逻辑
lv_result = 'CC=198' .
** 设置返回值
server->response->set_cdata( lv_result ).
server->response->set_content_type( content_type = 'text/html; charset=utf-8' ).
ELSEIF lv_method = 'POST'.
* {"AA":"1","BB":"22"}
lv_content_type = server->request->get_header_field( 'Content-Type' ).
CHECK lv_content_type+0(16) = 'application/json' .
** 获得输入参数
CALL METHOD server->request->get_cdata
RECEIVING
data = lv_parameter.
CHECK lv_parameter IS NOT INITIAL.
*** json 转 abap
CLEAR: lv_xdata,lv_xdata_o,lv_json .
CALL FUNCTION 'ECATT_CONV_STRING_TO_XSTRING'
EXPORTING
im_string = lv_parameter
im_encoding = 'UTF-8'
IMPORTING
ex_xstring = lv_xdata.
CALL FUNCTION 'ZCM_JSON_NAMES_UPPER' "十六进制JSON节点转为大写:SAP中字段都是大写的
EXPORTING
iv_in = lv_xdata
IMPORTING
ev_out = lv_xdata_o.
CALL FUNCTION 'ECATT_CONV_XSTRING_TO_STRING'
EXPORTING
im_xstring = lv_xdata_o
im_encoding = 'UTF-8'
IMPORTING
ex_string = lv_json.
DATA: lv_aa(10) TYPE c,
lv_bb(10) TYPE c.
CALL TRANSFORMATION id
SOURCE XML lv_json
RESULT aa = lv_aa
bb = lv_bb.
**************** 程序逻辑
lv_aa = lv_aa + 10 .
lv_bb = lv_bb + 100 .
*****************
*** abap 转 json
DATA(json_writer) = cl_sxml_string_writer=>create( type = if_sxml=>co_xt_json ).
DATA writer TYPE REF TO if_sxml_writer.
writer ?= json_writer.
writer->set_option( option = if_sxml_writer=>co_opt_linebreaks ).
writer->set_option( option = if_sxml_writer=>co_opt_indent ).
CALL TRANSFORMATION id
SOURCE cc = lv_aa
dd = lv_bb
RESULT XML json_writer.
DATA(json) = json_writer->get_output( ).
CLEAR lv_json .
CALL FUNCTION 'ECATT_CONV_XSTRING_TO_STRING'
EXPORTING
im_xstring = json
im_encoding = 'UTF-8'
IMPORTING
ex_string = lv_json.
CONDENSE lv_json NO-GAPS .
**** 设置返回值
server->response->set_cdata( lv_json ).
CALL METHOD server->response->set_header_field
EXPORTING
name = 'Content-Type'
value = 'application/json; charset=utf-8'.
ENDIF .
* 输出参数设计
* server->response->set_header_field( name = 'Pragma' value ='no-cache' ).
* server->response->set_header_field( name = 'Cache-Control' value ='no-store' ).
* server->response->set_header_field( name = 'Expires' value ='0' ).
* server->response->set_header_field( name = 'Access-Control-Allow-Origin' value = '*' ).
* server->response->set_status( code = 400 " Bad Request
* reason = lv_reason ).
ENDMETHOD.
火狐浏览器 httprequester插件
FUNCTION zcm_json_names_upper.
*"----------------------------------------------------------------------
*"*"本地接口:
*" IMPORTING
*" REFERENCE(IV_IN) TYPE XSTRING OPTIONAL
*" EXPORTING
*" REFERENCE(EV_OUT) TYPE XSTRING
*"----------------------------------------------------------------------
CHECK iv_in IS NOT INITIAL.
TRY .
DATA(reader) = cl_sxml_string_reader=>create( iv_in ).
DATA(writer) = CAST if_sxml_writer(
cl_sxml_string_writer=>create( type = if_sxml=>co_xt_json ) ).
DO.
DATA(node) = reader->read_next_node( ).
IF node IS INITIAL.
EXIT.
ENDIF.
IF node->type = if_sxml_node=>co_nt_element_open.
DATA(attributes) = CAST if_sxml_open_element(
node )->get_attributes( ).
LOOP AT attributes ASSIGNING FIELD-SYMBOL(<attribute>).
IF <attribute>->qname-name EQ 'name'.
<attribute>->set_value(
to_upper( <attribute>->get_value( ) ) ).
ENDIF.
ENDLOOP.
ENDIF.
writer->write_node( node ).
ENDDO.
ev_out = CAST cl_sxml_string_writer( writer )->get_output( ) .
CATCH cx_sxml_parse_error cx_sxml_state_error.
ENDTRY.
ENDFUNCTION.
DATA(json_writer) = cl_sxml_string_writer=>create( type = if_sxml=>co_xt_json ).
DATA writer TYPE REF TO if_sxml_writer.
writer ?= json_writer.
writer->set_option( option = if_sxml_writer=>co_opt_linebreaks ).
writer->set_option( option = if_sxml_writer=>co_opt_indent ).
CALL TRANSFORMATION id
SOURCE cc = lv_aa
dd = lv_bb
RESULT XML json_writer.
DATA(json) = json_writer->get_output( ).
CLEAR lv_json .
CALL FUNCTION 'ECATT_CONV_XSTRING_TO_STRING'
EXPORTING
im_xstring = json
im_encoding = 'UTF-8'
IMPORTING
ex_string = lv_json.
CLEAR: lv_xdata,lv_xdata_o,lv_json .
CALL FUNCTION 'ECATT_CONV_STRING_TO_XSTRING'
EXPORTING
im_string = lv_parameter
im_encoding = 'UTF-8'
IMPORTING
ex_xstring = lv_xdata.
CALL FUNCTION 'ZCM_JSON_NAMES_UPPER' "十六进制JSON节点转为大写:SAP中字段都是大写的
EXPORTING
iv_in = lv_xdata
IMPORTING
ev_out = lv_xdata_o.
CALL FUNCTION 'ECATT_CONV_XSTRING_TO_STRING'
EXPORTING
im_xstring = lv_xdata_o
im_encoding = 'UTF-8'
IMPORTING
ex_string = lv_json.
DATA: lv_aa(10) TYPE c,
lv_bb(10) TYPE c.
CALL TRANSFORMATION id
SOURCE XML lv_json
RESULT aa = lv_aa
bb = lv_bb.