Last active
February 13, 2018 06:43
-
-
Save quxiaowei/5537e4ecb295df359c7d269fb9a58e1a to your computer and use it in GitHub Desktop.
[Restful Handler] #ABAP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
METHOD if_rest_handler~handle. | |
DATA: BEGIN OF wa_jsondata, | |
name TYPE char20, | |
greeting TYPE char100, | |
END OF wa_jsondata. | |
DATA str_name TYPE string. | |
str_name = io_request->get_uri_attribute( iv_name = 'name' ). | |
str_name = cl_http_utility=>if_http_utility~unescape_url( str_name ). | |
wa_jsondata-name = str_name. | |
wa_jsondata-greeting = |hello { wa_jsondata-name }, 你好!|. | |
DATA: lo_response TYPE REF TO if_rest_response, | |
lo_entity TYPE REF TO if_rest_entity, | |
lv_current_time TYPE timestamp. | |
GET TIME STAMP FIELD lv_current_time. | |
lo_entity = io_response->create_entity( ). | |
lo_entity->set_modification_date( iv_modification_date = lv_current_time ). | |
lo_entity->set_content_compression( abap_true ). | |
DATA: writer TYPE REF TO cl_sxml_string_writer, | |
v_text TYPE string, | |
v_json TYPE xstring. | |
writer = cl_sxml_string_writer=>create( type = if_sxml=>co_xt_json ). | |
CALL TRANSFORMATION ztransform_json2 SOURCE out = wa_jsondata | |
RESULT XML writer. | |
v_json = writer->get_output( ). | |
v_text = cl_abap_codepage=>convert_from( v_json ). | |
DATA conv TYPE REF TO cl_abap_conv_out_ce. | |
conv = cl_abap_conv_out_ce=>create( encoding = 'UTF-8' ). | |
conv->convert( | |
EXPORTING data = v_text | |
IMPORTING buffer = v_json ). | |
lo_entity->set_content_type( iv_media_type = 'application/json' ). | |
lo_entity->set_binary_data( v_json ). | |
lo_entity->set_header_field( iv_name = 'charset' iv_value = 'utf-8' ). | |
io_response->set_status( 200 ). | |
ENDMETHOD. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment