说明:这里没有使用ODATA,只是用最基础的HTTP和JSON来处理。
这篇为第一篇,所以这里来说说验证的一些东西。
创建SE24实施类,做http接口:ZCRM_REST
激活后,添加接口类:IF_HTTP_EXTENSION
添加实现代码:
METHOD if_http_extension~handle_request. TYPES:BEGIN OF ty_login, username TYPE string, password TYPE string, END OF ty_login, BEGIN OF ty_retu, success TYPE string, access_token TYPE string, message TYPE string, END OF ty_retu. DATA: lo_json_ser TYPE REF TO zcl_trex_json_serializer, lo_json_des TYPE REF TO zcl_trex_json_deserializer. DATA: lt_fields TYPE tihttpnvp, lv_data TYPE string, lv_retu TYPE string, gw_login TYPE ty_login, gw_retu TYPE ty_retu, lv_guid TYPE crmt_object_guid. FIELD-SYMBOLS: <fs_field> LIKE LINE OF lt_fields. *""GET HTTP MESSAGE BODY lv_data = server->request->if_http_entity~get_cdata( ). zprocessjson=>deserialize( EXPORTING json = lv_data CHANGING data = gw_login ). "CHECK AUTH ADN SET RETURN MESSGAE IF gw_login-username = 'andy' AND gw_login-password = '123456'. gw_retu-success = 'true'. CALL FUNCTION 'CRM_GUID_CREATE' IMPORTING ev_guid = lv_guid. gw_retu-access_token = lv_guid. ELSE. gw_retu-success = 'E'. gw_retu-message = 'ERROR'. ENDIF. lv_retu = /ui2/cl_json=>serialize( data = gw_retu pretty_name = 'L' compress = abap_true ). "SET RETURN DATA server->response->set_cdata( EXPORTING data = lv_retu " Character data ). ENDMETHOD.
sicf在sap目录下创建服务API
保存,激活,测试服务。
哎,尽管能取到token,但是再后续调用的过程中,header传递的token取不到。。。操。。。这就不好玩了呀。那这样REST接口只能用用户名和密码验证了。。。突然。。。索然无味
^_^很多人都把资料锁进了note里,以后还怎么百度呢^_^