头图

Create a new handler folder under the productservice folder:

Create a new AdminService.java file in this folder:

This class now handles READ and CREATE events for the Products entity of the AdminService.

  • A READ operation simply returns all entities held in memory.
  • The CREATE event extracts the payload from the CQN representation and stores it in memory.

CDS Query Notation (CQN) is the common language in CAP for running queries against services. It can be used to communicate with services defined by the model, as well as with remote services such as databases.

Event handlers use the following APIs, which are available to service providers in CAP Java:

  1. The event handler class must implement the marker interface, marker interface EventHandler and register itself as Spring Beans (@Component). Marking the interfaces is important because it enables the CAP Java runtime to recognize these classes in all Spring beans.
  2. Event handler methods are registered with the @Before, @On, or @After annotations. Every event, such as entity creation, goes through these three phases. The semantics of each stage are slightly different.
  3. The annotation @ServiceName specifies the default service name applicable to all event handler methods. This is AdminService because that's also the name when the service is defined in the CDS model.

Event handler methods get event-specific event context parameters that provide access to event input parameters and the ability to set results. For example, let's look at the CdsCreateEventContext context parameter. The event we want to extend is the CREATE event. The type of context variable is specific to this extended CREATE event. The onCreate method returns void because the result is set by running: context.setResult(…).

Use the command line mvn clean spring-boot:run start the template application, and then use the curl command on the command line to insert a new product data:

curl -X POST http://localhost:8080/odata/v4/AdminService/Products \
-H "Content-Type: application/json" \
-d '{"ID": 42, "title": "My Tutorial Product", "descr": "You are doing an awesome job!"}'

Inserted successfully:

Open the Java application url:
https://workspaces-ws-pdwk4-app1.us10.trial.applicationstudio.cloud.sap
You can see the product just inserted:


注销
1k 声望1.6k 粉丝

invalid