Skip to Content

Add a Custom Event Handler

Write your first CAP Java Custom Event Handler.
You will learn
  • How to write a custom event handler for CAP Java
  • Which event handler classes and methods are available

In the following tutorials, you will learn that the CAP Java runtime can handle all CRUD events (create, read, update, and delete) triggered by OData requests out of the box. For now, we’ll show you how to do this manually, so that you can see how to write a custom event handler to extend the event handling process.

  • Step 1
    1. Create the Java package, by creating a new folder called handlers under srv/src/main/java/customer /products_service.

      package for Custom Event Handlers
    2. Create the Java class file AdminService.java in the created handlers folder, with the following content and make sure you Save the file:

    Java
    Copy
    package customer.products_service.handlers;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import org.springframework.stereotype.Component;
    
    import com.sap.cds.services.cds.CdsCreateEventContext;
    import com.sap.cds.services.cds.CdsReadEventContext;
    import com.sap.cds.services.cds.CqnService;
    import com.sap.cds.services.handler.EventHandler;
    import com.sap.cds.services.handler.annotations.On;
    import com.sap.cds.services.handler.annotations.ServiceName;
    
    @Component
    @ServiceName("AdminService")
    public class AdminService implements EventHandler {
    
        private Map<Object, Map<String, Object>> products = new HashMap<>();
    
        @On(event = CqnService.EVENT_CREATE, entity = "AdminService.Products")
        public void onCreate(CdsCreateEventContext context) {
            context.getCqn().entries().forEach(e -> products.put(e.get("ID"), e));
            context.setResult(context.getCqn().entries());
        }
    
        @On(event = CqnService.EVENT_READ, entity = "AdminService.Products")
        public void onRead(CdsReadEventContext context) {
            context.setResult(products.values());
        }
    
    }
    

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

    • The READ operation just returns all entities kept 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 to run queries against services. It can be used to talk to the services defined by your model, but also remote services, such as the database.

    The event handler uses the following APIs, which are available for service providers in CAP Java:

    • Event handler classes have to implement the marker interface EventHandler and register themselves as Spring Beans (@Component). The marker interface is important, because it enables the CAP Java runtime to identify these classes among all Spring Beans.
    • You register your Event Handler Methods with @Before, @On, or @After annotations. Every event, such as an entity creation, runs through these three phases. Each phase has a slightly different semantic. You will learn more about these semantics in the subsequent tutorial.
    • The annotation @ServiceName specifies the default service name all event handler methods apply to. Here this is AdminService, as this was also the name when defining the service in the CDS model.
    • Event handler methods get an event-specific event context parameter, which provides access to the input parameters of the event and the ability to set the result. For example, let’s look at the CdsCreateEventContext context parameter. The event we’re extending is the CREATE event. The type of the context variable is specific to this extended CREATE event. The onCreate method returns void, as the result is set by running: context.setResult(…).

    Does the method name of a custom event handler matter?

  • Step 2

    Stop your application if it’s still running by using CTRL+C in the terminal.

  • Step 3
    1. Restart the application by running the following command in the terminal:

      Shell/Bash
      Copy
      cd ~/projects/products-service && mvn clean spring-boot:run
      
    2. Choose Open in New Tab when prompted.

      open application in new tab message

      A new Browser tab is opened with your application.

      application opened
  • Step 4

    Try to insert some data into the running application. For example, by using the HTTP request plugin bundled in SAP Business Application Studio.

    1. Create a new file requests.http in the root directory.

    2. Add the following request to the file:

      HTTP
      Copy
      ### Create Product
      
      POST http://localhost:8080/odata/v4/AdminService/Products
      Content-Type: application/json
      
      {"ID": 42, "title": "My Tutorial Product", "descr": "You are doing an awesome job!"}
      

      The POST request causes an OData Insert on the entity Products of the service AdminService. The type of the content is specified in the Content-Type header of the HTTP request and the content of the actual request is passed in the body of the request as JSON.

    3. Choose Send Request above the request in the file. You will see the result on the right side of the window.

      response is displayed on the right
  • Step 5
    1. To read the data again, open the welcome page of the application.

    2. Choose Products from the app welcome page or add /odata/v4/AdminService/Products to the app URL.

    link on welcome page

    You should see something like this:

    records for Products entity

    This is the record you have inserted in the previous step through the HTTP request.

    If the data isn’t formatted the way it is shown in the screenshot, use the JSON Formatter extension for Google Chrome or another JSON Formatter for your preferred browser. The data itself should be the same anyway.

    Great Job! You have successfully added custom logic to handle specific requests. The next tutorial will show you how to extend the application and build the products service from scratch.

    In addition, you will use an actual database as the persistence and see some of the features the CAP Java SDK provides out of the box, without a single line of custom coding.


Back to top