ABAP 发送JSON报文

调用主函数: 

FUNCTION ZWMS_MATERIAL_POST.
*"----------------------------------------------------------------------
*"*"本地接口:
*"  IMPORTING
*"     REFERENCE(INPUT) TYPE  ZWMSP009
*"  EXPORTING
*"     REFERENCE(OUTPUT) TYPE  ZWMSP010
*"----------------------------------------------------------------------
  DATA:servicename  TYPE string,
       requestid    TYPE string,
       sourcesystem TYPE string,
       targetsystem TYPE string.
  DATA:lv_url    TYPE string,
       lv_reqstr TYPE string.
  DATA:return TYPE string.

  DATA:lo_json_ser TYPE REF TO cl_trex_json_serializer.
  DATA:json_string TYPE string.

  TYPES BEGIN OF TY_JSON.
    TYPES:ITEMS TYPE STANDARD TABLE OF zwmsp009_i WITH DEFAULT KEY.
  TYPES END OF TY_JSON.

  DATA:lt_json TYPE TY_JSON.

  MOVE-CORRESPONDING input-ITEMS TO lt_json-ITEMS.

*  "将数据转换为json格式
      CLEAR:json_string.
  json_string = /ui2/cl_json=>serialize( data = lt_json compress = abap_true pretty_name = /ui2/cl_json=>pretty_mode-camel_case ).

*从配置表取接口地址
  SELECT SINGLE
    zaddress
    FROM zmmt001
    INTO lv_url
    WHERE zmandt = sy-mandt
      AND znumber = 'WMS001'.
  IF lv_url IS INITIAL.
    output-type     = 'F'.
    output-message  = '请维护接口配置!'.
*添加到接口监控平台
    zcl_iface_h_record=>save( input = input output = output ).
    EXIT.
  ENDIF.

  lv_reqstr = json_string.

  DATA:lv_uuid TYPE sysuuid_c36.
  CALL METHOD cl_system_uuid=>if_system_uuid_static~create_uuid_c36
    RECEIVING
      uuid = lv_uuid.

  servicename    =   'A0001'.
  requestid      =   lv_uuid.
  sourcesystem   =   'SAP'.
  targetsystem   =   'WMS'.

  CALL FUNCTION 'ZJSON'
    EXPORTING
      i_url                      = lv_url
      i_reqstr                   = lv_reqstr
      servicename                = servicename
      requestid                  = requestid
      sourcesystem               = sourcesystem
      targetsystem               = targetsystem
    IMPORTING
      e_retstr                   = return
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2
      http_processing_failed     = 3
      OTHERS                     = 4.
  IF sy-subrc <> 0."接口调用失败
    output-type     = 'F'.
    output-message  = '接口调用失败!'.
  ELSE.
*接口调用成功,返回json转abap
    DATA:ls_output TYPE zpps005."需要包含header里的字段
    CALL METHOD /ui2/cl_json=>deserialize
      EXPORTING
        json        = return
        pretty_name = 'X'
      CHANGING
        data        = ls_output.

    IF ls_output-responsehead-errorcode = 'S'.
      output-type = 'S'.
    ELSEIF ls_output-responsehead-errorcode = 'E'.
      output-type     = 'E'.
      output-message  = ls_output-responsehead-errordesc.
    ELSE.
      output-type     = 'E'.
      output-message  = return.
    ENDIF.
  ENDIF.
*接口日志
  zcl_iface_h_record=>save( input = input output = output ).

ENDFUNCTION.

函数:

FUNCTION zjson.
*"----------------------------------------------------------------------
*"*"本地接口:
*"  IMPORTING
*"     VALUE(I_URL) TYPE  STRING
*"     VALUE(I_REQSTR) TYPE  STRING
*"     VALUE(SERVICENAME) TYPE  STRING OPTIONAL
*"     VALUE(REQUESTID) TYPE  STRING OPTIONAL
*"     VALUE(SOURCESYSTEM) TYPE  STRING OPTIONAL
*"     VALUE(TARGETSYSTEM) TYPE  STRING OPTIONAL
*"  EXPORTING
*"     REFERENCE(E_RETSTR) TYPE  STRING
*"  EXCEPTIONS
*"      HTTP_COMMUNICATION_FAILURE
*"      HTTP_INVALID_STATE
*"      HTTP_PROCESSING_FAILED
*"----------------------------------------------------------------------

  DATA: lr_http_client TYPE REF TO if_http_client .
  DATA: lv_result      TYPE string,
        lv_post_string TYPE string,
        lv_len         TYPE i,
        lt_post_string LIKE STANDARD TABLE OF lv_post_string.

  CLEAR:lv_post_string.

  TRANSLATE i_reqstr TO UPPER CASE."字符小写转大写
  REPLACE 'ITEMS' WITH 'items' INTO i_reqstr.

  lv_post_string = i_reqstr.
*  lv_len = cl_abap_list_utilities=>dynamic_output_length( lv_post_string )."计算字符串字节长度
  lv_len = strlen( lv_post_string ).

* 创建对象
  CALL METHOD cl_http_client=>create_by_url
    EXPORTING
      url                = i_url
    IMPORTING
      client             = lr_http_client
    EXCEPTIONS
      argument_not_found = 1
      plugin_not_active  = 2
      internal_error     = 3
      OTHERS             = 4.
  lr_http_client->propertytype_logon_popup = lr_http_client->co_enabled .


* 设定相关参数
  CALL METHOD lr_http_client->request->set_header_field
    EXPORTING
      name  = '~request_method'
      value = 'POST'.

  CALL METHOD lr_http_client->request->set_header_field
    EXPORTING
      name  = 'Content-Type'
      value = 'application/json;charset=utf-8'.

*  CALL METHOD lr_http_client->request->set_header_field
*    EXPORTING
*      name  = 'Content-Type'
*      value = 'application/json'.


  CALL METHOD lr_http_client->request->set_header_field
    EXPORTING
      name  = 'Accept'
      value = 'text/html,*/*'.
*    VALUE = 'application/json'.
*设置抬头传参字段(具体接口具体处理)
  CALL METHOD lr_http_client->request->set_header_field
    EXPORTING
      name  = 'serviceName'
      value = serviceName.

  CALL METHOD lr_http_client->request->set_header_field
    EXPORTING
      name  = 'requestId'
      value = requestId.

  CALL METHOD lr_http_client->request->set_header_field
    EXPORTING
      name  = 'sourceSystem'
      value = sourceSystem.

  CALL METHOD lr_http_client->request->set_header_field
    EXPORTING
      name  = 'targetSystem'
      value = targetSystem.

* 设置请求报文
  CALL METHOD lr_http_client->request->set_cdata
    EXPORTING
      data   = lv_post_string
      offset = 0
      length = lv_len.

* 构建HTTP连接,以及发送数据
  CALL METHOD lr_http_client->send
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2.
  IF sy-subrc NE 0.
    IF sy-subrc = 1.
      MESSAGE s398(00) WITH 'http_communication_failure' RAISING http_communication_failure.
    ELSEIF sy-subrc = 2.
      MESSAGE s398(00) WITH 'http_invalid_state' RAISING http_invalid_state.
    ENDIF.
    RETURN.
  ENDIF.
* 接口HTTP响应
  CALL METHOD lr_http_client->receive
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2
      http_processing_failed     = 3.
  IF sy-subrc NE 0.
    IF sy-subrc = 1.
      MESSAGE s398(00) WITH 'http_communication_failure' RAISING http_communication_failure.
    ELSEIF sy-subrc = 2.
      MESSAGE s398(00) WITH 'http_invalid_state' RAISING http_invalid_state.
    ELSEIF sy-subrc = 3.
      MESSAGE s398(00) WITH 'http_processing_failed' RAISING http_processing_failed.
    ENDIF.
    RETURN.
  ENDIF.
* 取响应报文
  e_retstr = lr_http_client->response->get_cdata( ).

* 关闭HTTP连接
  CALL METHOD lr_http_client->close.


ENDFUNCTION.

 

你可能感兴趣的:(ABAP,ABAP,JSON,ABAP,HTTP)