Service Definitions Part 1: What a Service Definition Does
Free sample lesson · open to everyone
Learn what a service definition is in RAP, what it contains and what it does not, and where it sits between projections and service bindings.
In this lessonShow sectionsHide sections
Expert visual walkthrough
What a Service Definition Does
Service Definitions · Chapter-specific technical teaching map
Architecture hierarchy
Projection entities
Service definition
Exposed entity names
Service binding
Tutor's chapter takeaway
Learn what a service definition is in RAP, what it contains and what it does not, and where it sits between projections and service bindings.
In a real project
A new team member was asked to "add OData V2 for the old UI5 app" and started copying the whole service definition, projections included. The lead explained that the service definition only describes content; the protocol is…
Watch out for
• Exposing base R_ views instead of projections. • Expecting the service definition to control operations or UI. • Copying service definitions just to change the protocol. • Technical aliases (ZC_PURCHREQ) instead of business…
The situation: a real project scenario
A new team member was asked to "add OData V2 for the old UI5 app" and started copying the whole service definition, projections included. The lead explained that the service definition only describes content; the protocol is a binding property.
He created a second binding (OData V2 – UI) on the existing ZUI_PURCHREQ definition — ten minutes of work, no duplicated objects, and both apps received exactly the same entities.
Explanation
A service definition lists which entities a service exposes, using syntax like "define service … { expose … as …; }". It contains no logic and no protocol — the binding is what decides OData V2 or V4, UI or API. The entities exposed should be projection views (ZC_/ZA_), never base R_ views or raw tables, and anything not exposed is simply invisible to consumers, including as a navigation target.
This series covers service definitions across six parts: Part 1, this lesson, on what a service definition does; Part 2 on exposing projections as a business service; Part 3 on API boundary, naming and versioning; Part 4 on troubleshooting activation and exposure gaps; Part 5 on UI versus integration APIs and whether to use one service or two; and Part 6 on exposing multiple entities, navigation and value helps. The next topic after this series is Service Bindings.
In the overall chain, a table becomes a CDS base view (ZR_) plus a BDEF, which becomes a projection (ZC_) plus a projection BDEF, which becomes a service definition, which becomes a service binding, which becomes an actual OData service reachable by a Fiori app or API client. The service definition is the first object in that chain that answers the consumer's question: "what can I access in this service?"
A service definition's content looks like this:
define service ZUI_PURCHREQ {
expose ZC_PurchReq as PurchaseRequest;
expose ZC_PurchReqItem as PurchaseRequestItem;
}
Here, "expose <CDS entity> as <alias>" turns each exposed entity into an entity set in OData. The alias is the name consumers actually see, so it should be a business name rather than a technical one. Optional header annotations, such as @EndUserText.label, can also be added.
What a service definition deliberately does not contain is just as important: it has no behavior, since operations come from the projection BDEF; no UI, since layout comes from annotations and metadata extensions; no protocol or URL, since that's the job of the service binding (OData V2/V4, UI/Web API); and no authorization, which is handled by the business object, CDS access control and roles.
It exists as a separate object because one service definition can be bound several times — for example with both a V4 UI binding and a V4 Web API binding — and several service definitions can reuse the same projections. Keeping "what" (the definition) separate from "how" (the binding) means the protocol can change without touching the content at all.
To build one yourself: in ADT, right-click ZC_PurchReq and choose New → Service Definition; name it ZUI_PURCHREQ with a description like "Purchase Request UI service"; expose the root and item projections using business aliases, as shown in the code example; and activate it. The next step is creating the binding, covered in Service Bindings Part 2.
With this you can write a service definition and explain its role in one sentence: it defines the content of a service, and nothing else.
Code example
// ===== Minimal RAP service definition =====@EndUserText.label: 'Purchase Request - UI service' // description shown in the service binding / catalog, purely descriptivedefine service ZUI_PURCHREQ { // one service definition can bundle several related projections expose ZC_PurchReq as PurchaseRequest; // root projection -> entity set expose ZC_PurchReqItem as PurchaseRequestItem; // child projection -> entity set} // ===== The chain around it =====// ZPR_HEAD / ZPR_ITEM tables// ZR_PurchReq / ZR_PurchReqItem base BO (CDS + BDEF)// ZC_PurchReq / ZC_PurchReqItem projections (CDS + projection BDEF)// ZUI_PURCHREQ service definition <- WHAT is exposed// ZUI_PURCHREQ_O4 service binding <- HOW (OData V4 - UI) // ===== WRONG =====// expose ZR_PurchReq as PurchaseRequest; // base BO exposed: no projection control// expose zpr_head as PurchaseRequest; // tables cannot be exposedWatch out
Common mistakes
- Exposing base R_ views instead of projections.
- Expecting the service definition to control operations or UI.
- Copying service definitions just to change the protocol.
- Technical aliases (ZC_PURCHREQ) instead of business names.
- Forgetting to expose child or navigation target entities.
Do this
Best practices
- One service definition per business service and consumer type.
- Expose only projections, with business aliases.
- Keep protocol decisions in bindings.
- Label the service definition with @EndUserText.label.
- Name consistently: ZUI_… for UI services, ZAPI_… for APIs.
Apply it: production case clinic
Two decisions · one practical outcomeUse a fictional SAP incident to practise choosing the next safe investigation. This is a worked diagnostic exercise, separate from the lesson's existing knowledge-check questions.
ERPClimb SAP case file · SERVICE DEFINITIONS
Identify exactly which CDS projection entities belong to a service.
Incident brief
A child item CDS projection exists but external clients cannot find an Item entity set in the published OData metadata.
Evidence available
- Root and child projections are activated
- Only the root is listed in the service definition
- OData $metadata lacks the intended Item entity set
Decision 1 of 2
What is the first contract-layer correction?
Apply in a permitted SAP DEV / test environment
Compare the projected entity list with define service { expose ... as ...; } and $metadata; document the missing alias.
Evidence of success: The root and intended child entity sets are represented in the published service contract.
Release consideration: Association exposure and projection redirection are additional requirements for working navigation.
Consult official SAP guidance (opens new tab)Local, self-directed practice only. No SAP system is connected, no persistent grade is awarded, and no production changes are made.
In the interview
Interview angle
Questions: "What is a service definition?", "Service definition vs binding?", "What can be exposed?"
Strong answer: a service definition lists the CDS entities (projections) exposed by a service, with aliases; it has no behavior, UI or protocol — the binding defines protocol (OData V2/V4) and type (UI/Web API). One definition can have several bindings. Base views and tables should not be exposed.
Practice this lesson
Quick check
0 of 3 questions attempted
Question 1 of 3
What does a RAP service definition specify?
Question 2 of 3
Which object should normally be exposed in a RAP service definition?
Question 3 of 3
Which binding type fits a system-to-system integration using a RAP service?
Practise this in an interview
Answer in your own words and get instant feedback, the way an interviewer would judge it.
1Describe the complete path from a RAP business object to a running OData service.
2What is a RAP service definition, and how does it differ from a service binding?
See it in code
Each example starts from a requirement, shows the design, then the code step by step with a plain-language explanation of every part.
Was this lesson helpful?
Keep exploring
Check before use: lesson examples are educational, not a guarantee of correctness for your SAP edition or release. Sample code has not been executed on your SAP system. Confirm API availability, syntax, authorizations and business impact in an approved development or test environment.