Develop a CAP JAVA App Using SAP Business Application Studio
- How to create a CAP project
- How to develop business applications based on the SAP Cloud Programming Model (CAP)
- How to run and test your application using the Run Configurations tool
Prerequisites
- You have access to SAP Business Application Studio (see Set Up SAP Business Application Studio for Development).
- You have created a Full-Stack Application Using Productivity Tools dev space as described in Create a Dev Space for Business Applications.
The application you’ll develop is a simple bookshop app consisting of a data model with three entities:
- Books
- Authors
- Genres
The data model is exposed via the Catalog Service. The application has some initial data that is used for testing the application, and some custom logic that runs after reading the books from the Books entity.
Once you have all the code in place, you will test the application locally.
- Step 1
-
From the SAP Business Application Studio hamburger menu, select File > New Project from Template.
You can also go to the Command Palette and choose SAP Business Application Studio: New Project From Template.
-
Select the CAP Project template, and click Start.

-
Enter
bookshopas the name for the project. -
From the Select your runtime dropdown list, select Java.
-
Select the SAP HANA Cloud checkbox.

-
Click Finish.
The project is generated.
-
From the SAP Business Application Studio hamburger menu, select File > Open Folder.
-
In the command palette, open the projects folder.

-
Select your project and click OK.

The Storyboard is displayed.

-
- Step 2
-
In the Data Models tile, click + to create a data model entity.

-
Click Create.

It may take a few moments for the Graphical Modeler to be populated.
-
Change the entity name to Books.

-
Click on the entity and then click on the
(Show Details) icon.

-
In the Properties pane, click + to add new properties.

-
Add the following properties:
Name Type title String descr String stock Integer price Decimal The Books entity should look like this:

-
Click Add Entity.

-
Rename the new entity Authors.
-
In the Properties pane, click + to add a new property.

-
Add the following property:
Name Type name String -
Click Add Entity.

-
Rename the new entity Genres.
-
Click on the Authors entity and then click on the Add Relationship icon.

-
Drag the arrow to the Books entity.

-
In the Relationship Details dialog, select the To-One radio button for the Cardinality and click OK.

-
Click on the Genres entity and then click on the Add Relationship icon.

-
Drag the arrow to the Books entity.

-
In the Relationship Details dialog, select the Composition radio button for the Type.
-
Select the To-Many radio button for the Cardinality and click OK.

-
- Step 3
-
In the storyboard, go to the Service tile, click + to create a service entity.
-
Click Create.

-
Click on the service and select Add Service Entity.

-
From the Projection dropdown list, select Bookshop.Books.
-
Click
to save your changes.

-
In the storyboard, click on the service and select Add Action/Function.

-
Change the name of the action to submitOrder.

-
In the Properties pane, add the following:
Parameter Name Parameter Type amount Integer books_id Integer 
-
- Step 4
-
In the storyboard, go to the Data Models tile, click on Authors, and select Add Data.

-
In the Data Editor, enter 3 for the number of rows with mock data and click Add.

-
Repeat the procedure to add rows for the Genres and the Books entities.
-
In the command palette, search for the
application.yamlfile.

-
Add the following line in the
cdssection for the default profile.JavaCopydata-source.csv.paths : "test/data/**" -
Save your changes.
The file should look like this:

This configuration tells the application where the sample data is located within the project structure.
-
- Step 5
-
Go to the Explorer.
-
Navigate to
srv>src>main>java / customer / bookshop, and create a new folder calledhandlers.
-
Add a new file in the
handlersfolder calledBookshopServiceHandler.java.
-
Populate the
BookshopServiceHandler.javafile with the following:JavaCopypackage customer.bookshop.handlers; import org.springframework.stereotype.Component; import com.sap.cds.Result; 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.After; import com.sap.cds.services.handler.annotations.ServiceName; @Component @ServiceName("bookshopService") public class BookshopServiceHandler implements EventHandler { @After(event = CqnService.EVENT_READ, entity = "bookshopService.Books") public void onRead(CdsReadEventContext context){ Result result = context.getResult(); //result.forEach(r -> System.out.println(r.get("title"))); result.forEach(r -> { if( ((Integer) r.get("stock"))> 111) r.put("title",((String)r.get("title")).concat(" -- Discount")); }); } }To learn more about working with event handlers, see Event Handlers.
-
Save your changes.
Your application should look similar to the structure shown in the picture below.

You can also see the semantic structure of the application in the Project Overview.

What is the Project Overview?
-
- Step 6
You can explicitly deploy your application to a persistent local
SQLitedatabase, or you can run your application and it will implicitly use an in-memory database.This step describes how to run the application with an in-memory database.
You will first add all required dependencies, and then create and run a run configuration.
-
Add and install all required dependencies.
-
From the Terminal menu, select New Terminal.
-
On the
bookshopfolder, run the following:NPMCopynpm install
-
-
From the Activity pane, open the Run Configurations view.

-
Click + at the top of the view to add a new configuration.

-
Select
Bookshop - (CAP Java)as the runnable application from the command palette prompt.
There might be other run configuration options available in the command palette.
-
Press
Enterto use the default name for the configuration. A new configuration is added to the run configuration tree. -
In the Configuration editor, select the Default Profile radio button for the database type.

-
Click the green arrow on the right of the configuration name to run the application.

-
If prompted, open the application in a new tab.

The application opens in the browser.
-
Click on Books to see the metadata and entities for the service.

You can also debug your application to check your code logic. For example, to debug the custom logic for this application, perform the following steps:
-
Place a breakpoint in the function in the
service.jsfile. -
In the running app, click the
Booksentity. It should stop at the breakpoint.
-
Click Continue in the debugger until all the books are read and the page is presented.

-
Remove the breakpoint.
-
Stop the application by clicking Stop in the Debugger. The number beside the Debug icon represents the number of running processes. Click Stop until there are no processes running.
-
- Step 7
Prerequisites
-
Make sure you have an SAP HANA database available in your space. See Create an SAP HANA Database Instance Using SAP HANA Cloud Central
-
You have an SAP HANA service (SAP HANA as a Service or SAP HANA Cloud) available in your space.
To create a new instance in your trial account:- Go to your space in the SAP BTP cockpit.
- Go to Service Marketplace.
- Select SAP HANA Schemas & HDI Containers.
- Click Create.

- From the Plan dropdown menu, select
hdi-sharedas the service plan and provide a name for the new instance. - Click Create.
-
From the Activity pane, open the Run Configurations view.

-
Click + at the top of the view to add a new configuration.

-
Select
Bookshop - (CAP Java)as the runnable application from the command palette prompt.
There might be other run configuration options available in the command palette.
-
Press
Enterto use the default name for the configuration. A new configuration is added to the run configuration tree. -
In the Configuration editor, select the SAP HANA Cloud radio button for the database type.

-
Log in to Cloud Foundry.
-
From the SAP Cloud Instance dropdown list, select the relevant instance.
-
From the Deploy the data model before running dropdown list, select the Deploy (with data) radio button.

-
Click the green arrow on the right of the run configuration.

-
If prompted, open the application in a new tab.

The application opens in the browser.
-
From the terminal on the bookshop folder, run
cds add mta. This adds anmta.yamlfile to the root of your application.Note: If you are working on a trial account, open the
mta.yamlfile, and in theresourcessection change theserviceparameter tohana. Save your changes. -
Right-click the
mta.yamlfile and choose Build MTA Project.
A new folder for
mta_archivesis created containing the newmtarfile. -
Right-click the
mtarfile and choose Deploy MTA Archive.
Once the task is complete, your application should be available in your Cloud Foundry space.
To access your application, go to your space in the SAP Cloud Platform cockpit and select Applications from the side menu. -