ABAP 创建Rest接口实例

2022-05-10 19:47:29 浏览数 (1)

  1. 创建Rest接口 创建类实现接口IF_HTTP_EXTENSION。
代码语言:javascript复制
  METHOD if_http_extension~handle_request.
    DATA(lv_method) = server->request->get_method( )."获取调用方法
    CASE lv_method.
      WHEN 'GET'.
        me->get( server ).
      WHEN 'POST'.
        me->post( server ).
      WHEN 'OPTIONS'.
       me->options( server ).
      WHEN OTHERS.
        server->response->set_status( code =  '405' reason = 'test' ).
    ENDCASE.
  ENDMETHOD.
  *在不同的调用方法中,写逻辑,先解析传来的数据,处理后,返回数据
      DATA:lv_json    TYPE string,
    lv_JSON = server->request->get_cdata( ).//获取传过来的参数
    lv_json = server->request->get_content_type( ).//获取RequestHeader的Content-Type
    /ui2/cl_json=>deserialize( EXPORTING json = lv_json CHANGING data = lt_data ).//将json格式的参数转为内表或其他参数
    //转为内表后,处理完数据,就可以写返回数据
    server->response->set_status( code =  '200' reason = '成功' ).//设定返回的状态
    lv_re_json = zcl_data_to_json=>data_to_json( lv_message ).//将返回的数据再转为json格式
    server->response->set_cdata( lv_re_json ).//发送返回的json格式信息	
    server->response->set_content_type( 'application/json' ).//设定返回的内容类型

创建完后,用T-code:SICF,创建一个服务,在处理器清单中,添加刚才的类,保存激活后,就生成了服务。

右键点击,选择测试服务,复制浏览器中的地址,就是接口的地址

  1. 调用Rest接口
代码语言:javascript复制
DATA: lv_len           TYPE i, "发送报文长度
      lv_url           TYPE string, "接口地址
      lo_http_client   TYPE REF TO if_http_client, "http客户端
      lv_send_json     TYPE string, "发送的JSON
      lv_rec_json      TYPE string, "接收的JSON
      lv_authorization TYPE string,
      lv_code          TYPE i, "HTTP 返回状态
      lv_reason        TYPE  string. " HTTP 状态描述
  "先创建http客户端
  CALL METHOD cl_http_client=>create_by_url
    EXPORTING
      url                = lv_url "上面的地址
    IMPORTING
      client             = lo_http_client
    EXCEPTIONS
      argument_not_found = 1
      plugin_not_active  = 2
      internal_error     = 3
      OTHERS             = 4.
        "不显示登录界面
  lo_http_client->propertytype_logon_popup = lo_http_client->co_disabled.
  "验证用户名和密码
    CALL METHOD LO_HTTP_CLIENT->AUTHENTICATE
    EXPORTING
      USERNAME = Lv_USERNAME
      PASSWORD = Lv_PASSWORD.
”设定调用方法 GET,POST   
    lo_http_client->request->set_method(
    	if_http_request=>co_request_method_get  ).
"设定传输请求内容格式以及编码格式
    lo_http_client->request->set_content_type(
      content_type = 'application/json; charset=utf-8'
    ).  	
 ”发送前数据准备   
 lv_send_json = /ui2/cl_json=>serialize( lt_data )."转发送的数据为JSON格式
   lv_len = strlen( lv_send_json ).
    CALL METHOD lo_http_client->request->set_cdata
      EXPORTING
        data   = lv_send_json
        offset = 0
        length = lv_len.   
  "发送
  CALL METHOD lo_http_client->send
    EXCEPTIONS
      http_communication_failure = 1                  " Communication Error
      http_invalid_state         = 2                  " Invalid state
      http_processing_failed     = 3                  " Error when processing method
      http_invalid_timeout       = 4                  " Invalid Time Entry
      OTHERS                     = 5.
  "接收
  CALL METHOD lo_http_client->receive
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2
      http_processing_failed     = 3.    	
   "获取返回的状态信息
  CLEAR:lv_code,lv_reason.
  CALL METHOD lo_http_client->response->get_status
    IMPORTING
      code   = lv_code
      reason = lv_reason.
  "获取返回的JSON
  CLEAR:lv_rec_json,ls_token.
  lv_rec_json = lo_http_client->response->get_cdata( ).
  /ui2/cl_json=>deserialize( EXPORTING json = lv_rec_json
                              CHANGING  data = ls_token ).
  "关闭接口
  CALL METHOD lo_http_client->close.     

0 人点赞