Step by step process for creating a rest API (Web Service) in sap, which will be consume by
web based application:
1. Create Table- ZDT_WEB_COMPLAIN
2. Create SNRO Number range object: ZSNRO_WEB
Transaction code: SNRO
Give SNRO name and click on create
enter short text, long text, number length domain, % warning and save.
click on Ranges--> click on change intervals--> click on new entries.
insert the number range and save.
3. Create request handler class : ZCL_WEBCOMPLAINT_REQHANDLR
Go to Transaction Code - SE24 and create request handler class.
enter the superclass as cl_rest_http_handler and click on save.
Go to method section to see super class methods.
select the get_root_handler and click on redefine. Add the below code in between method and endmethod.
DATA(lo_router) = NEW cl_rest_router( ).
lo_router->attach( EXPORTING
iv_template = '/webcomplaint' " Unified Name for Reso
iv_handler_class = 'ZCL_WEBCOMPLAINT_REQHANDLR' " Object Type Name
" it_parameter = " Resource contructor parameters save and activate the class .
4. Create resource provider class: ZCL_WEBCOMPLAINT_REQPROVIDER.
Go to Transaction Code - SE24 and create request provider class.
enter the superclass as cl_rest_resource and click on save.
Go to method section to see super class methods.
select the method 'GET' and click on redefine . Add the below code.
DATA : lv_string1 TYPE vbeln, "string,
lv_string2 TYPE string,
gs_complaint TYPE zdt_web_complain.
lv_string1 = mo_request->get_uri_query_parameter( iv_name = 'DOC_ID' ).
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = lv_string1
IMPORTING
output = lv_string1.
SELECT SINGLE * FROM zdt_web_complain INTO CORRESPONDING FIELDS OF gs_complaint
WHERE doc_id = lv_string1.
/ui2/cl_json=>serialize(
EXPORTING
data = gs_complaint " Data to serialize
* COMPRESS = ABAP_FALSE " Skip empty elements
* NAME = " Object name
* PRETTY_NAME = " Pretty Print property names
* TYPE_DESCR = " Data descriptor
RECEIVING
r_json = lv_string2 " JSON string
).
mo_response->create_entity( )->set_string_data( iv_data = lv_string2 ).
mo_response->set_header_field(
EXPORTING
iv_name = 'Content-Type' " Header Name
iv_value = 'application/json' " Header Value
).
save and activate .
similarly redefine the post method and add the below code.
METHOD if_rest_resource~post.
*CALL METHOD SUPER->IF_REST_RESOURCE~POST
* EXPORTING
* IO_ENTITY =
* .
DATA : lv_string1 TYPE vbeln, "string,
lv_string2 TYPE string,
lv_response TYPE string,
gs_complaint TYPE zdt_web_complain.
DATA(lo_entity) = mo_request->get_entity( ).
DATA(lo_response) = mo_response->create_entity( ).
"read string data i.e json
DATA(lv_data) = lo_entity->get_string_data( ).
/ui2/cl_json=>deserialize(
EXPORTING
json = lv_data " JSON string
* PRETTY_NAME = " Pretty Print property names
CHANGING
data = gs_complaint " Data to serialize
).
* CATCH CX_SY_MOVE_CAST_ERROR. "
CALL FUNCTION 'NUMBER_GET_NEXT'
EXPORTING
nr_range_nr = '01'
object = 'ZSNRO_WEB'
quantity = '1'
* SUBOBJECT = ' '
* TOYEAR = '0000'
* IGNORE_BUFFER = ' '
IMPORTING
number = gs_complaint-doc_id
* QUANTITY =
* RETURNCODE =
EXCEPTIONS
interval_not_found = 1
number_range_not_intern = 2
object_not_found = 3
quantity_is_0 = 4
quantity_is_not_1 = 5
interval_overflow = 6
buffer_overflow = 7
OTHERS = 8.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
gs_complaint-createdby = sy-uname.
gs_complaint-createdon = sy-datum.
gs_complaint-time = sy-uzeit.
INSERT INTO zdt_web_complain VALUES gs_complaint.
/ui2/cl_json=>serialize(
EXPORTING
data = gs_complaint " Data to serialize
* COMPRESS = ABAP_FALSE " Skip empty elements
* NAME = " Object name
* PRETTY_NAME = " Pretty Print property names
* TYPE_DESCR = " Data descriptor
RECEIVING
r_json = lv_response
).
" JSON string
lo_response->set_string_data( iv_data = lv_response ).
ENDMETHOD.
save and activate.
similarly redefine the post method and add the below code.
METHOD if_rest_resource~post.
*CALL METHOD SUPER->IF_REST_RESOURCE~POST
* EXPORTING
* IO_ENTITY =
* .
DATA : lv_string1 TYPE vbeln, "string,
lv_string2 TYPE string,
lv_response TYPE string,
gs_complaint TYPE zdt_web_complain.
DATA(lo_entity) = mo_request->get_entity( ).
DATA(lo_response) = mo_response->create_entity( ).
"read string data i.e json
DATA(lv_data) = lo_entity->get_string_data( ).
/ui2/cl_json=>deserialize(
EXPORTING
json = lv_data " JSON string
* PRETTY_NAME = " Pretty Print property names
CHANGING
data = gs_complaint " Data to serialize
).
* CATCH CX_SY_MOVE_CAST_ERROR. "
CALL FUNCTION 'NUMBER_GET_NEXT'
EXPORTING
nr_range_nr = '01'
object = 'ZSNRO_WEB'
quantity = '1'
* SUBOBJECT = ' '
* TOYEAR = '0000'
* IGNORE_BUFFER = ' '
IMPORTING
number = gs_complaint-doc_id
* QUANTITY =
* RETURNCODE =
EXCEPTIONS
interval_not_found = 1
number_range_not_intern = 2
object_not_found = 3
quantity_is_0 = 4
quantity_is_not_1 = 5
interval_overflow = 6
buffer_overflow = 7
OTHERS = 8.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
gs_complaint-createdby = sy-uname.
gs_complaint-createdon = sy-datum.
gs_complaint-time = sy-uzeit.
INSERT INTO zdt_web_complain VALUES gs_complaint.
/ui2/cl_json=>serialize(
EXPORTING
data = gs_complaint " Data to serialize
* COMPRESS = ABAP_FALSE " Skip empty elements
* NAME = " Object name
* PRETTY_NAME = " Pretty Print property names
* TYPE_DESCR = " Data descriptor
RECEIVING
r_json = lv_response
).
" JSON string
lo_response->set_string_data( iv_data = lv_response ).
ENDMETHOD.save and activate.
5. Create SICF service:
Execute Transaction code SICF.
click on execute.
give the service element name and press enter.
enter description and request handler class name [ zcl_webcomplaint_reqhandlr ]and save.
on SICF main screen activate the service.
now before testing add some record in table, then test the service.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 19 | |
| 11 | |
| 8 | |
| 6 | |
| 5 | |
| 5 | |
| 3 | |
| 3 | |
| 3 | |
| 3 |