为了确保一切设置正确,本教程还包括如何构建和运行一个简单的 Hello World 应用程序。 SAP Cloud 应用程序编程模型 (CAP) 支持 Java 和 Node.js 开发。 但是对于本教程,我们使用的是 Java。 CAP Java SDK 能够与 Spring Boot 紧密集成,Spring Boot 提供了许多开箱即用的功能。 这意味着,Spring Boot 将成为您的运行时容器。
打开 SAP Business Technology Platform Trial 账号,进入 Business Application Studio:

打开之前创建好的 dev space,如果没有,新创建一个。

类型要选择成 Full Stack Cloud Application:

进入 space 之后,我们在浏览器里看到了一个类似 Visual Studio Code 的在线编辑器,打开一个新的命令行窗口:

我们当前的工作目录为:/home/user/projects:

运行如下命令行:
mvn -B archetype:generate -DarchetypeArtifactId=cds-services-archetype -DarchetypeGroupId=com.sap.cds
-DarchetypeVersion=RELEASE
-DgroupId=com.sap.cap -DartifactId=products-service -Dpackage=com.sap.cap.productsservice
如果遇到这个错误:mvn: command not found,说明之前创建的 space 类型不正确,应该选择 fullstack Cloud Application:

mvn 命令执行完毕后,应该看到如下输出:

这将使用 maven archetype cds-services-archetype 初始化应用程序并创建您的项目。
该项目被命名为 products-service.
- db 文件夹存储与数据库相关的工件。
- srv 文件夹存储您的 Java 应用程序。
打开名为 products-service 的工作空间。

CAP 应用程序使用核心数据服务 (CDS) 来描述:
- 使用实体定义来描述数据结构
- 使用服务定义来描述如何消费数据结构
下面我们创建一个简单的 service,其会定义属于自己的实体。
创建一个新文件:admin-service.cds

源代码如下:

service AdminService {
entity Products {
key ID : Integer;
title : String(111);
descr : String(1111);
}
}
命令行里执行 mvn clean install,确保看到 build success 消息:

运行此命令后,会生成一些文件并将其添加到 srv/src/main/resources/edmx 文件夹中。 这是默认路径,CAP Java 运行时在其中查找模型定义。

如您所见,该文件不包含特定于 CAP 的启动指令。 这是每个 Spring Boot 应用程序中的典型样板代码。 CAP Java 运行时的初始化由 Spring 根据 pom.xml 中定义的依赖关系自动完成。现在可以启动这个 SpringBoot 应用了:

运行命令行:mvn clean spring-boot:run

点击 Expose and open,就能在浏览器里看到应用了:

expose port 的含义:该应用程序没有任何可从 Internet 访问的端口。 只有 SAP Business Application Studio 中的容器正在侦听某个尚未从 Internet 访问的端口。
更多Jerry的原创文章,尽在:"汪子熙":

网友评论