|
所在平台: Udemy |
课程主页: https://www.udemy.com/course/build-a-server-side-restful-api-with-coldbox-merapi/
课程评论:没有评论
课程名称:使用Coldbox构建服务器端RESTful API(Merapi) 课程概述:本课程将指导学员如何构建一个使用Coldbox框架的REST API,并利用TestBox模块进行测试。学员将学习如何通过在API处理程序中添加注释来为API进行文档化,并使用Coldbox的cbSwagger模块生成Swagger编辑器中的API文档。课程中,学员将使用Postman输入数据,以模拟对开发服务器的API调用响应。需要注意的是,课程不详细讲解Swagger编辑器和Postman工具,以及使用Coldbox cbSecurity和JWT(Json Web Tokens)的API安全问题。此外,课程不涉及与此服务器端API连接的JavaScript前端的构建。 课程将首先向MySQL数据库模式Merapi添加四个表,构建表之间的关系,并利用Coldbox的cfmigrations模块通过种子数据填充表。该数据库模式管理与国家、货币、城市和机场相关的数据。虽然cfmigrations模块在之前的两个教程中已被详细覆盖,但本教程开头会再次介绍并在附带的指南中进行记录。 接下来,学员将构建模型,并使用代表数据库模式的XML架构文件将实体类属性名称映射到表的列名称。我们还将使用XML加载器文件读取XML架构,实现一个简单的自定义对象关系映射器。此方法作为替代方案,与之前的课程Tamarind v1中使用Quick ORM的方式不同,课程中不涉及ORM。 我们还通过创建BaseService父组件来利用对象继承。像数据访问对象(DAO)一样,此组件为我们的查询提供了抽象,包括读取、删除和记录过滤方法。这样,就不需要在子组件中重复编写这些查询,我们只需使用Super关键字从BaseService父组件调用相关方法。 为了确保模型实体和服务的功能完整性,我们将使用Testbox模块编写单元测试。在完成模型实体和服务组件并确保所有单元测试通过后,我们将专注于构建API本身。API将涉及API处理程序和API文档两个方面。 处理程序包括多个动作(索引、显示、创建、更新和删除)。在编写处理程序的同时,我们也将编写相应的集成测试,以确保处理程序运行正常。集成测试同样使用Testbox编写。API文档由json文件组成,包括响应、请求体、参数和基于API响应(如2xx、4xx、5xx)的示例。每个处理程序动作代码的注释中都会指向/apidocs文件夹中的这些json文件。 完成以上步骤后,我们将借助Coldbox的cbswagger模块,在开发服务器url(cbswagger)上生成输出。将此cbswagger输出复制粘贴到Swagger编辑器中,不仅可以生成整洁美观的API文档,还能生成一个openAPI.yaml文件,后者可以导入到Postman中。Postman是一种允许用真实数据进一步测试我们的API服务器的工具。 课程结束时,学员应能够扎实理解如何使用Coldbox框架构建服务器端REST API的实际操作。
We are going to build a Coldbox REST API and test it using the TestBox module. We shall document this API by adding annotations to our API handlers. We'll then use the Coldbox cbSwagger module to generate our API documentation in the Swagger Editor. Finally, we'll use Postman to input data in order to simulate API responses from calls to our development server. Please note however, that it is not in the scope of this course to explain Swagger Editor and Postman tools in detail. Also note that API security issues using Coldbox cbSecurity and JWT (Json Web Tokens) won't be discussed in this course. Last and not least, we won't build and connect a javaScript front-end to this server-side API.We are first going to add tables to our MySQL database schema called Merapi (4 tables altogether) , build relationships between those tables and populate our tables with seed data with the help of Coldbox cfmigrations module. This database schema manages data about Countries, Currencies, Cities and Airports. The cfmigrations module was already covered in detail in my two earlier tutorials, but will be covered again at the beginning of this tutorial and documented in the guide provided with it.Next, we are going to build our model and map our entity class property names to our tables columns name using an XML schema file representing the database schema. We'll also have an XML Loader file to read the XML schema, thus implementing a simple custom object relational mapper. This approach is an alternative, to what we demonstrated in another course called Tamarind v1, in which we used Quick ORM to map entity object properties to table columns and to load entities in memory. Therefore, in this course, no ORM is involved.We also leveraged object inheritance with the creation of a BaseService parent component. Like a DAO (Data Access Object), this component provides an abstraction of our queries for READ, DELETE and record filtering methods. This way, there is no need to write those queries again in the children components. We simply call the relevant method from the BaseService parent component with the Super keyword.To make sure that our model entity and services are fully functional, we shall use the Testbox module to write unit tests. After completing our model entity and service components and making sure all our unit tests pass, we shall focus on building the API itself. The API is going to deal with two types of things: the API handlers and the API documentation.The handlers are composed of actions (index, show, create, update and delete). As we write our handlers, we shall also write the corresponding integration tests that guarantee that our handlers are working fine. Integration tests will also be written with Testbox. The API documentation is composed of json files such as responses, requestBody, parameters and examples that apply to each handler's action based on an API response such as 2xx (Success), 4xx (Not found or validation error) or 5xx (Server error). A pointer to each of these json files found in the /apidocs folder, will be added as an annotation to the handler's action code. Thanks to this, our API documentation will become immediately exportable to tools such as Postman or OpenAPI (Swagger).Once this is done, we shall leverage the Coldbox cbswagger module to generate an output as we hit our development server url at cbswagger. Copying and pasting this cbwagger output in our Swagger Editor will not only deliver a colourful and neat documentation of our API, but also generate an openAPI.yaml file that we can then import into Postman. Postman is a tool that allows further testing of our API server with real data. At the end of the course, you should have acquired a solid and practical understanding as how to build a server-side REST API with the Coldbox framework.