0% found this document useful (0 votes)
252 views15 pages

Rest Api Step by Step

The document provides steps to create a RESTful web service in SAP ABAP to handle customer complaints. It involves creating a table to store complaints, a number range object for unique IDs, a request handler class, and a resource class with GET and POST methods to retrieve and create complaints respectively. The service is tested using Postman after creating a SICF service.

Uploaded by

Aleax Rao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
252 views15 pages

Rest Api Step by Step

The document provides steps to create a RESTful web service in SAP ABAP to handle customer complaints. It involves creating a table to store complaints, a number range object for unique IDs, a request handler class, and a resource class with GET and POST methods to retrieve and create complaints respectively. The service is tested using Postman after creating a SICF service.

Uploaded by

Aleax Rao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Page |1

create rest based service (web service) in sap


abap BY - BIJAYA KRUSHNA ROUT
Step by step process for creating a rest api (web service ) in sap, which will be consume by
web based application :

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


Page |2

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


Page |3

give the object id as ZRESTC and click on create

enter short text, long text, number length domain, % warning and save.
Page |4

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


Page |5

give the short description and click on create inheritance

enter the superclass as cl_rest_http_handler and click on save.


Page |6

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
urces
IV_HANDLER_CLASS = 'ZCL_WEBCOMPLAINT_RP' " Object Type Name

* IT_PARAMETER = " Resource contructor parameters


).

RO_ROOT_HANDLER = lo_router.
Page |7

save and activate the class

4: Create resource provider class : Transaction SE24

enter the class id and click on create.


Page |8

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


Page |9

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 G


S_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(
P a g e | 10

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
P a g e | 11

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
P a g e | 12

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

click on execute.
P a g e | 13

select default_host and click on create host/ service

give the service id and press enter.


P a g e | 14

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.
P a g e | 15

Thank you...

Kindly share your feedback ...

You might also like