Project Requirement :
1.
Create a rest based service, which
will be called by customer portal or mobile app for creating the
customer complaint.
2.
Generate unique id for each complaint
and send the created id in response.
3.
If complaint id pass through portal,
send the details status of the complaint.
Data will be pass from portal:
1.
Customer Name
2.
Mobile Number
3.
Email ID
4.
Complaint description
In response sent the complaint number
Development approach:
1.
Create a table.
2.
Create SNRO Number range object for
complaint id.
3.
Create request handler class - ZCL_WEBCOMPLAINT_RH
4.
Create resource class and implement
get and post method.
5.
Create SICF service and activate
6.
Test the complete cycle with postman.
1: Create Table
Transaction Code -SE11 - ZREST_COMPLAINT
2: Create SNRO Number range object : ZRESTC
Transaction code : SNRO
enter short text, long text, number length domain, % warning and save.
click on Ranges
click on change intervals
click on insert line
insert the, number range and save.
3: Create request handler class :
Transaction Code - SE24
give the class id ZCL_WEBCOMPLAINT_RH and click on create
give the short description and click on create inheritance
enter the superclass as cl_rest_http_handler and click on save.
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 Resources
IV_HANDLER_CLASS = 'ZCL_WEBCOMPLAINT_RP' " Object Type Name
* IT_PARAMETER = " Resource contructor parameters
).
RO_ROOT_HANDLER = lo_router.
save and activate the class
4: Create resource provider class : Transaction SE24
enter the class id and click on create.
enter description, click on create inheritance cl_rest_resource and click on save.
select the method 'GET' and click on redefine .
add the below code
method IF_REST_RESOURCE~GET.
*CALL METHOD SUPER->IF_REST_RESOURCE~GET
* .
data : lv_string1 type vbeln, "string,
lv_string2 type string,
gs_complaint type ZREST_COMPLAINT.
lv_string1 = mo_request->GET_URI_QUERY_PARAMETER( iv_name = 'ID' ).
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
INPUT = lv_string1
IMPORTING
OUTPUT = lv_string1.
select SINGLE * from zrest_complaint into CORRESPONDING FIELDS OF GS_COMPLAINT
WHERE 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
).
endmethod.
save and activate .
similar 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 ZREST_COMPLAINT.
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 = 'ZRESTC'
QUANTITY = '1'
* SUBOBJECT = ' '
* TOYEAR = '0000'
* IGNORE_BUFFER = ' '
IMPORTING
NUMBER = GS_COMPLAINT-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 ZREST_COMPLAINT 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 the class.
5: Create SICF service : Transaction SICF
select default_host and click on create host/ service
give the service id and press enter.
enter description and handler class and save. zcl_webcomplaint_rh
on sicf main screen activate the service.
now before testing add one record in our table , then test the service.
do the testing as per process given in the demo videos.
part 1 of 2
part 2 of 2
Thank you.....
Kindly share your feedback ...
5 Comments
Thanx brother for your valuable video, can you make an demo on how to call this web service from outside SAP like VB, VB6, Android etc.?
ReplyDeleteThanks in advance... best regards
another request, please make an demo with post request from SAP/VB6 etc.
ReplyDeleteVery nicely you explained and demonstrated
ReplyDeleteNice post. Can you expand on the security and credentials side of this. Automated API's won't be logging in manually. How do you pass credentials securely in the api calls?
ReplyDeletePLEASE HELP TO CONNECT AS THE POSTMAN APP
ReplyDelete