{"id":7671,"date":"2025-08-27T15:36:50","date_gmt":"2025-08-27T10:06:50","guid":{"rendered":"https:\/\/www.sapewmhelp.com\/?question=how-to-create-a-web-api-using-custom-entity"},"modified":"2025-08-27T15:36:50","modified_gmt":"2025-08-27T10:06:50","slug":"how-to-create-a-web-api-using-custom-entity","status":"publish","type":"question","link":"https:\/\/www.sapewmhelp.com\/?question=how-to-create-a-web-api-using-custom-entity","title":{"rendered":"How to create a web api using custom entity"},"content":{"rendered":"<p>A <strong>Custom Entity<\/strong> in RAP is like a <strong>virtual data model<\/strong> \u2014 it does <strong>not persist data in a DB table<\/strong>, but fetches it from logic (e.g., CDS views, APIs, function modules, or custom code).<\/p>\n<p>It is exposed via a <strong>behavior definition<\/strong> and then published as an <strong>OData\/Web API service<\/strong>.<\/p>\n<\/p>\n<p><strong>1. Create a Custom Entity in CDS<\/strong><\/p>\n<p>@EndUserText.label: &#8216;Custom Employee API Entity&#8217;<br \/>\ndefine custom entity ZC_EMPLOYEE_API {<br \/>\nkey empid : abap.char(10);<br \/>\nempname : abap.char(40);<br \/>\ndepartment: abap.char(20);<br \/>\n}<\/p>\n<\/p>\n<p><strong>2. Define a Behavior Implementation (Handler Class)<\/strong><\/p>\n<p>CLASS zcl_employee_handler DEFINITION<br \/>\nPUBLIC FINAL CREATE PUBLIC.<br \/>\nPUBLIC SECTION.<br \/>\nINTERFACES if_rap_query_provider.<br \/>\nENDCLASS.<\/p>\n<p>CLASS zcl_employee_handler IMPLEMENTATION.<br \/>\nMETHOD if_rap_query_provider~select.<br \/>\nDATA lt_data TYPE TABLE OF zc_employee_api.<\/p>\n<p>&#8220;Fetch data (from table, FM, RFC, etc.)<br \/>\nSELECT empid, empname, dept AS department<br \/>\nFROM zemployees<br \/>\nINTO TABLE @lt_data.<\/p>\n<p>&#8220;Return data<br \/>\nresult = CORRESPONDING #( lt_data ).<br \/>\nENDMETHOD.<br \/>\nENDCLASS.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>3. Register Service in Service Definition<\/strong><\/p>\n<p>@EndUserText.label: &#8216;Employee API Service&#8217;<br \/>\ndefine service ZUI_EMPLOYEE_API {<br \/>\nexpose ZC_EMPLOYEE_API;<br \/>\n}<\/p>\n<p>&nbsp;<\/p>\n<p><strong>4. Publish via Service Binding<\/strong><\/p>\n<ul>\n<li>Create a <strong>Service Binding<\/strong> (<code>ZUI_EMPLOYEE_API_BIND<\/code>).<\/li>\n<li>Choose <strong>OData V4<\/strong> as binding type.<\/li>\n<li>Activate it \u2192 SAP generates a URL endpoint.<\/li>\n<\/ul>\n<p>\/sap\/opu\/odata4\/sap\/zui_employee_api\/srvd_a2x\/sap\/zui_employee_api\/0001\/<\/p>\n<p>&nbsp;<\/p>\n<p><strong>5. Test the API<\/strong><\/p>\n<ul>\n<li>\n<p>Use <code>\/IWFND\/MAINT_SERVICE<\/code> (for OData V2 in SEGW) or Fiori Preview (for OData V4).<\/p>\n<\/li>\n<li>\n<p>Run the URL in browser or Postman:<\/p>\n<\/li>\n<\/ul>\n<p>GET &#8230;\/ZC_EMPLOYEE_API<\/p>\n<p>&nbsp;<\/p>\n<h5>Summary<\/h5>\n<ul>\n<li>\n<p><strong>Custom Entity<\/strong> = Virtual CDS entity (no table).<\/p>\n<\/li>\n<li>\n<p>Implement a <strong>handler class<\/strong> to supply data.<\/p>\n<\/li>\n<li>\n<p>Expose it in a <strong>service definition<\/strong>.<\/p>\n<\/li>\n<li>\n<p>Bind it with <strong>service binding<\/strong> (OData V2\/V4).<\/p>\n<\/li>\n<li>\n<p>Test via Fiori preview or Postman.<\/p>\n<\/li>\n<\/ul>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","question-category":[175],"question_tags":[178,201,199,200,177],"class_list":["post-7671","question","type-question","status-publish","hentry","question-category-rap","question_tags-abap","question_tags-btp","question_tags-cds","question_tags-rap","question_tags-sap"],"_links":{"self":[{"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=\/wp\/v2\/question\/7671","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=\/wp\/v2\/question"}],"about":[{"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=\/wp\/v2\/types\/question"}],"author":[{"embeddable":true,"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7671"}],"wp:attachment":[{"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7671"}],"wp:term":[{"taxonomy":"question-category","embeddable":true,"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fquestion-category&post=7671"},{"taxonomy":"question_tags","embeddable":true,"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fquestion_tags&post=7671"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}