Step-by-Step
Procedure for Creating RAP APIs in SAP Following the Clean Core Approach
A
Detailed Guide to Modern SAP Application Development
Introduction
The RESTful ABAP Programming model (RAP) represents a
paradigm shift in SAP development, enabling the creation of cloud-ready,
service-oriented applications using ABAP. When paired with the Clean Core
approach, RAP empowers organizations to build extensible, maintainable, and
upgrade-friendly solutions that avoid modifications to SAP’s standard codebase.
This document provides an in-depth, step-by-step procedure for creating RAP
APIs in SAP, emphasizing best practices to maintain a clean core architecture.
Prerequisites
·
SAP S/4HANA system (on-premises or cloud) or SAP
BTP, ABAP Environment
·
Developer access with necessary roles and
authorizations
·
Understanding of ABAP and SAP Fiori concepts
·
Familiarity with CDS (Core Data Services), BOPF,
and OData services
·
Access to Eclipse with ABAP Development Tools
(ADT) or SAP Business Application Studio
Step 1: Define the Domain Model Using CDS Entities
Step 1: Define the Root View Entity
A fundamental concept in RAP is the Root View Entity, which
serves as the primary data source and entry point for your RAP business object.
The root view encapsulates both the main business entity and its core data
model, forming the backbone of your API.
To create a Root View Entity:
-
In ADT/Eclipse, create a new CDS Data Definition
and designate it as a 'view entity' using the DEFINE ROOT VIEW ENTITY
statement.
-
Specify all essential fields, keys, and
business-relevant attributes in the entity definition. The entity should
accurately reflect the business semantics of the process you are modelling.
-
Annotate the root view with RAP-specific
annotations such as @EndUserText.label for documentation and @ObjectModel for behaviour
and service exposure.
-
Ensure the root view entity is modelled in a
custom namespace (e.g., zrap_*) to comply with clean core principles and
facilitate future extensibility.
-
The root view may define associations to other
dependent or reusable entities, but it should remain the authoritative source
for the business object’s lifecycle.
With the root view entity defined, you establish a solid
foundation for the RAP business object, ensuring consistency, reusability, and
alignment with SAP’s clean core architecture.
Step 2: Create a Behaviour Definition (Managed or Unmanaged)
Behavior definitions in RAP specify the operations (CRUD,
business logic, validations) available on the data model.
Create Behavior Definition Artifact:
·
Use ADT to create a Behavior Definition (BDEF)
for your root CDS entity.
·
Choose managed for standard CRUD or unmanaged
for complex, custom logic.
·
Define operations such as create, update,
delete, and custom actions.
·
Specify validations, determinations, and
authorization checks within the behavior definition.
Step 3: Implement Behavior in Behavior Implementation (BIMP)
Separate implementation logic in dedicated classes to ensure
extensibility and clean upgrades.
Create Behavior Implementation Artifact:
·
Generate a Behavior Implementation (BIMP) object
for your BDEF.
·
In the ABAP class, implement the methods for
each operation (e.g., create, update, delete, custom actions).
·
Leverage RAP’s framework methods to minimize
custom code.
·
Maintain all custom logic in the local (Z/Y
namespace) class, avoiding modification of SAP standard objects.
Step 4: Define Service Definition and Service Binding
Service definitions expose RAP business logic as OData
services or APIs.
Create Service Definition:
·
In ADT, generate a Service Definition for the
root CDS entity and its associations.
·
Specify which entities, associations, and
actions are to be exposed.
·
Using annotations to restrict access or define
consumption scenarios (e.g., @OData.publish: true).
Create Service Binding:
-
Generate a Service Binding to bind the service
definition to an OData protocol (e.g., OData V2, OData V4, Web API).
-
Activate the service and take note of its URL
endpoint for consumption.
Once you have created and activated your service binding,
the API becomes accessible as an OData endpoint. To publish the API, ensure the
following steps are completed:
·
Register the Service: In SAP Gateway or your RAP
environment, register the service binding if system configuration requires it.
This step often involves using the relevant transaction (such as
/IWFND/MAINT_SERVICE in SAP GUI) or managing it directly in ADT.
·
Activate and Secure the Endpoint: Confirm that
the service binding is active. Assign appropriate roles and authorizations to
control access, using authorization objects or business catalogs as defined
during the service setup.
·
Document the Service URL: The activated binding
will provide you with an OData service URL. This URL is the entry point for API
consumers and can be shared with frontend developers or integration partners.
Step 5: Implement Extensibility and Business Logic via In-App Extensions
Clean Core demands that all customizations occur via
extensibility mechanisms such as In-App Extensions, not by direct modification.
Use Key User Tools:
·
Enable extensions (e.g., custom fields, logic,
business rules) using SAP Fiori’s Key User Extensibility tools.
·
Avoid classic user exits, BADIs, or modifying
standard code; use whitelisted APIs and extension points.
Implement Business Logic via Local Classes:
-
When additional custom logic is required,
encapsulate it within local classes under custom namespace.
-
Reference only the published (whitelisted) SAP
APIs and objects.
Step 6: Secure the API
Security is integral to RAP and Clean Core. Apply
authentication, authorization, and audit mechanisms.
Configure Authorization Objects:
·
Use CDS authorization annotations
(@AccessControl) to restrict data access at entity level.
·
Configure OData service authorization in SAP
Gateway or ABAP environment.
Implement Authentication:
-
Leverage SAP’s standard authentication
mechanisms (OAuth, SAML, Basic Auth) for API access.
Step 7: Test and Validate RAP API
Thorough testing ensures reliability, maintainability, and
adherence to Clean Core principles.
·
Use REST/OData Tools: Employ tools like Postman,
SAP Gateway Client, or any browser-based REST client to send requests to API’s
endpoint. We can perform CRUD operations by issuing HTTP methods (GET, POST,
PUT, DELETE) against the API’s URLs.
·
Metadata Validation: Append /$metadata to your
API endpoint to retrieve the service’s metadata document. This confirms that
entities, associations, and actions are correctly exposed.
·
Test Authorizations: Log in as users with
different roles to verify access restrictions and ensure authorization checks
are enforced as specified in your behavior and service definitions.
·
Examine Response Data: Validate the structure,
fields, and returned data for each operation. Ensure that custom logic and
determinations work as intended for create, update, and custom actions.
·
Monitor Logs and Errors: Use SAP’s monitoring
tools or transaction codes (like /IWFND/ERROR_LOG) to trace failures, inspect
logs, and troubleshoot issues during testing.
By following this publication and testing steps, you ensure
your RAP-based API is securely exposed, well-documented, and fully functional
before moving to productive use.
Step 8: Document the API and Provide Consumption Guidance
Comprehensive documentation facilitates integration and
long-term maintainability.
Describe API Endpoints:
·
Listing available endpoints, supported HTTP
methods, and payload structures.
·
Documenting authentication, authorization, error
codes, and custom actions.
Providing Examples:
·
Including sample requests and responses for
common scenarios.
·
Explain how to consume the API from external
applications (e.g., SAP Fiori, third-party clients).
Step 9: Maintain Clean Core through Upgrades and Extensions
Ensure that all future enhancements adhere to Clean Core
principles.
·
Monitor for Modification-Free Extensions:
·
Regularly audit custom developments for
compliance with SAP upgrade and modification-free requirements.
·
Leverage SAP tools to identify and remediate
direct changes to standard objects.
Conclusion
Developing RAP APIs in SAP while following the Clean Core
approach sets the foundation for future-proof, extensible, and stable
enterprise solutions. This methodology not only preserves upgrade agility and
system integrity but also aligns with SAP’s evolving cloud strategy. By
adhering to the steps and principles outlined above—CDS-based domain modelling,
managed extensibility, robust security, and thorough documentation—developers
can deliver APIs that are powerful, secure, and maintainable, ready to support
the business needs of tomorrow.
No comments:
Post a Comment