1.SE24新建类:ZCL_REST_QUERY

激活,然后添加interface:IF_HTTP_EXTENSION并激活。

2.实现IF_HTTP_EXTENSION~HANDLE_REQUEST:

  METHOD IF_HTTP_EXTENSION~HANDLE_REQUEST.
DATA: LT_FIELDS TYPE TIHTTPNVP,
LV_HEADER_QUERY TYPE STRING,
LV_DATA TYPE STRING,
LV_HTML TYPE STRING. 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
WITH KEY NAME = '~query_string'
ASSIGNING <FS_FIELD>.
IF SY-SUBRC EQ .
CONCATENATE '{"message": "You have entered'
<FS_FIELD>-VALUE
'as query parameter."}'
INTO LV_HTML SEPARATED BY SPACE. *" Output to HTML
SERVER->RESPONSE->SET_CDATA(
EXPORTING
DATA = LV_HTML " Character data
* offset = 0 " Offset into character data
* length = -1 " Length of character data
).
ENDIF.
ENDMETHOD.

3.事务代码SICF定义REST服务:

设置服务的用户名密码:

添加处理类:

保存激活.测试服务:

SAP调用测试:

创建程序:ZLYTEST_REST

REPORT zlytest_rest.
DATA: lo_http_client TYPE REF TO if_http_client,
lv_service TYPE string,
lv_request TYPE string,
lv_len TYPE i,
lv_result TYPE string,
lx_result TYPE xstring,
lo_ixml TYPE REF TO if_ixml,
lo_streamfactory TYPE REF TO if_ixml_stream_factory,
lo_istream TYPE REF TO if_ixml_istream,
lo_document TYPE REF TO if_ixml_document,
conv TYPE REF TO cl_abap_conv_in_ce,
lo_parser TYPE REF TO if_ixml_parser.
lv_service = 'http://gjyxcrmdev.crm.hisense.com:8000/sap/zrest'. lv_request = '<Request><Id>2912744</Id><Notes>testaskldjasd123</Notes><FaultSource>consumer</FaultSource>' &&
'<FaultBattery>no</FaultBattery>' &&
'<FaultScreen>no</FaultScreen>' &&
'<FaultFirmware>no</FaultFirmware>' &&
'<FaultLogicboard>no</FaultLogicboard>' &&
'<FaultKeyboard>no</FaultKeyboard>' &&
'<FaultSpeaker>no</FaultSpeaker>' &&
'<FaultHdd>no</FaultHdd>' &&
'<FaultPowersupply>no</FaultPowersupply>' &&
'<FaultOtherDescription>123</FaultOtherDescription>'
&& '</Request>'.
lv_len = strlen( lv_request ). cl_http_client=>create_by_url(
EXPORTING
url = lv_service
IMPORTING
client = lo_http_client
EXCEPTIONS
argument_not_found =
plugin_not_active =
internal_error =
OTHERS = ). *lo_http_client->authenticate(
* username = 'service@hisenseinhome.com.au'
* password = 'pass' ). CALL METHOD lo_http_client->request->set_cdata
EXPORTING
data = lv_request
offset =
length = lv_len. lo_http_client->send(
EXCEPTIONS
http_communication_failure =
http_invalid_state = ). lo_http_client->receive(
EXCEPTIONS
http_communication_failure =
http_invalid_state =
http_processing_failed = ). CLEAR lv_result .
lx_result = lo_http_client->response->get_data( ).
CALL METHOD lo_http_client->close.
CHECK = .

在REST处理类中添加断点,执行测试程序,测试结果如下:

数据已经传递过来了。

返回JSON数据格式例子:

    DATA: lo_json_ser TYPE REF TO cl_trex_json_serializer,
lo_json_des TYPE REF TO cl_trex_json_deserializer.
DATA: json_string TYPE string.
DATA:gt_t005u TYPE TABLE OF t005u.
"设置返回数据格式
CALL METHOD server->response->if_http_entity~set_content_type
EXPORTING
content_type = 'application/json'.
SELECT * INTO TABLE gt_t005u FROM t005u UP TO ROWS WHERE spras = '' AND land1 = 'CN'. CREATE OBJECT lo_json_ser
EXPORTING
data = gt_t005u[]. CALL METHOD lo_json_ser->serialize.
CALL METHOD lo_json_ser->get_data
RECEIVING
rval = json_string.
server->response->set_cdata(
EXPORTING
data = json_string " Character data
).

返回结果:

参考别人的:

  METHOD IF_HTTP_EXTENSION~HANDLE_REQUEST.
DATA: LO_JSON_SER TYPE REF TO CL_TREX_JSON_SERIALIZER,
LO_JSON_DES TYPE REF TO CL_TREX_JSON_DESERIALIZER.
DATA: JSON_STRING TYPE STRING.
DATA:GT_T005U TYPE TABLE OF T005U.
DATA:GW_T005U TYPE T005U.
DATA:LV_METHOD TYPE STRING. LV_METHOD = SERVER->REQUEST->GET_HEADER_FIELD( NAME = '~request_method' ). CASE LV_METHOD.
WHEN 'GET'.
SELECT * INTO TABLE GT_T005U FROM T005U UP TO ROWS WHERE SPRAS = '' AND LAND1 = 'CN'. CREATE OBJECT LO_JSON_SER
EXPORTING
DATA = GT_T005U[]. CALL METHOD LO_JSON_SER->SERIALIZE. CALL METHOD LO_JSON_SER->GET_DATA
RECEIVING
RVAL = JSON_STRING. CALL METHOD SERVER->RESPONSE->SET_CDATA(
EXPORTING
DATA = JSON_STRING ).
SERVER->RESPONSE->SET_STATUS( CODE = REASON = 'OK' ).
WHEN 'POST' OR 'PUT'.
JSON_STRING = SERVER->REQUEST->IF_HTTP_ENTITY~GET_CDATA( ).
CREATE OBJECT LO_JSON_DES.
CALL METHOD LO_JSON_DES->DESERIALIZE(
EXPORTING
JSON = JSON_STRING
IMPORTING
ABAP = GW_T005U ).
SERVER->RESPONSE->SET_STATUS( CODE = REASON = 'OK' ). * MODIFY GT_T005U FROM GW_T005U. WHEN 'DELETE'.
WHEN OTHERS.
ENDCASE. CALL METHOD SERVER->RESPONSE->IF_HTTP_ENTITY~SET_CONTENT_TYPE
EXPORTING
CONTENT_TYPE = 'application/json'. ENDMETHOD.

SAP发布REST/HTTP接口的更多相关文章

  1. 火车头wordpress免费万能发布模块和接口

    火车头wordpress免费万能发布模块和接口实测可以用 http://www.ggfenxiang8.com/?p=263

  2. IGS_学习笔记06_IREP发布客户化集成接口为Web Service(案例)

    2015-01-03 Created By BaoXinjian

  3. SAP FICO 凭证导入接口 数据xml格式

    接口传入参数说明 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xm ...

  4. 033.SAP上查看IDOC接口,PI接口查不到的日志记录,可能在IDOC接口日志里面

    01. SAP系统发料之后,数据没有传输到条码系统,同事也没有任何bc01的日志,这是就要考虑是不是在IDOC接口了,输入事务代码WE02或者WE05 02.双击查看内容 03.点开就能看到详细内容了 ...

  5. 2019年6月SAP发布的未来ABAP平台的发展方向

    未来ABAP平台将始终是这些产品的技术平台: S/4HANA On-Premises和Cloud将基于一个统一的ABAP codeline: SAP云平台上的ABAP编程环境: 什么是SAP Clou ...

  6. SAP发布wbservice,如果有权限管控的话,需要给这个webservice加权限

    1. PFCG床架角色 2.在角色菜单上,添加其他,选中Authorization Default Values for Services 如下图 3.选中发布的webservice 后保存,如下图: ...

  7. SAP PM:通过接口获取设备资产基本信息

    在SAP工厂维护模块中,给设备贴二维码标签是现在越来越流行的做法.因此通过扫描二维码获取设备资产信息是个非常基本的需求. 以下实例简单实现了,给SAP RFC传入设备编码获取设备资产基本信息的需求. ...

  8. java对接c++发布的webservice接口,其中参数类型有base64Binary格式(无需将图片数据转化为c++中的结构体)

    接口名称: std::string SendVehiclePass(std::string VehiclePassInfo, struct xsd__base64Binary PlatePicData ...

  9. SAP接口编程 之 JCo3.0系列(02) : JCo Client Programming

    SAP接口编程 之 JCo3.0系列(02) : JCo Client Programming 字数545 阅读52 评论0 喜欢1 JCo3.0调用SAP函数的过程 大致可以总结为以下步骤: 连接至 ...

随机推荐

  1. Linux 防火墙:Netfilter iptables

    一.Netfilter 简介 (1) Netfilter 是 Linux 内置的一种防火墙机制,我们一般也称之为数据包过滤机制,而 iptables 只是操作 netfilter 的一个命令行工具(2 ...

  2. selenium定位下拉框

    下拉选择框(Select) <div> <p>下拉选择框框 Select</p> <select id="proAddItem_kind" ...

  3. docker安装和基础操作

    docker安装 yum install docker 配置镜像下载加速器 curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh ...

  4. Ethzasl MSF源码阅读(3):MSF_Core和PoseMeasurement

    1.MSF_Core的三个函数:ProcessIMU.ProcessExternallyPropagatedState和AddMeasurement MSF_Core维护了状态队列和观测值队列,这里需 ...

  5. [js]jquery里的jsonp实现ajax异源请求

    同源请求-jquery <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/ ...

  6. 【UML】NO.55.EBook.8.UML.3.001-【UML和模式应用 第3版】

    1.0.0 Summary Tittle:[UML]NO.54.EBook.8.UML.3.001-[UML和模式应用 第3版] Style:DesignPattern Series:DesignPa ...

  7. 本地node启动服务步骤

    启动node服务: 1.git bash here (node server-run.js) 2.如果第一次启动node服务,要根据提示装依赖文件npm install 依赖文件名 3.启动成功提示: ...

  8. centos7.5固定局域网ip

    有点时候,比如像我们单位,没事干就停一次网,结果ip变了,还得重新看ip,重新配置,很麻烦,所以干脆把自己ip固定,以不变应万变!!! 1.首先查看自己的ip是什么: $ ifconfig eno1: ...

  9. 使用PageHelper插件分页结合mybatis返回的列表个数不对问题解决

    问题描述:spring mvc+mybatis项目中,当使用PageHelper插件进行分页查询时,查到的总数据量值是正确的,但是查询当前页返回的列表个数不对.比如每页查询10条,返回2条或者3条.r ...

  10. windows程序设计 显示一个窗口

    #include <windows.h> HINSTANCE hinst; LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM) ...