This is a 5 minutes tutorial how-to-use Oat++ web framework to build your own C++ performant web-service and integrate it with Swagger-UI.
- For Mac/Linux: Git, build-essentials, CMake.
- For Windows: Git, CMake, MSVC.
Clone oatpp repo:
For more detailed instructions see – installing Oat++ on Unix/Linux.
For more detailed instructions see – installing Oat++ on Windows.
Now we are able to build services using Oat++. It is a zero-dependency framework, so NO additional installations are required.
Build and Run Starter Project
Build “oatpp-starter” on Mac/Linux
Build “oatpp-starter” on Windows
Run compiled executable:
- Mac/Linux –
- Windows –
Now go to localhost:8000 and make sure that you can see the response from server
MyController class – contains declared endpoints and their info together with additional Swagger annotations.
MyDto class – resides in DTOs.hpp. Describes the Data-Transfer-Object used in the “Hello World” response mentioned above. In oatpp DTOs are used in ObjectMapping and Serialization/Deserialization.
App.cpp file – this is an applications’ entry point. Here Application Components are loaded, Controllers’ endpoints are added to the Router, and the server starts.
AppComponent class – basically it is a collection of components that will be loaded on application start. Here we configure things like which ConnectionProvider to use, port to listen to, which ObjectMapper to use.
To integrate Swagger-UI in the project we have to do the following:
- Clone and install oatpp-swagger module
- Add oatpp-swagger to CMakeLists.txt
- Add corresponding code to AppComponent.hpp and App.cpp
Add oatpp-swagger to CMakeLists.txt
Here we add
oatpp::swagger::Resources components which give general information about our API document and specify a path to Swagger-UI resources:
Here we add
oatpp::swagger::Controller to Router with the list of endpoints we want to document
Now if everything is ok, and
OATPP_SWAGGER_RES_PATH path is set correctly in the AppComponent.hpp, we should be able to build and run our project and see Swagger-UI at http://localhost:8000/swagger/ui in the browser.
Additional Info for Endpoint
Our endpoint is already present in the document with the proper method and path. Oat++ automatically documents most of the endpoints’ info, such as endpoint name, method, path, parameter names, and parameter types. However, there are things that should be specified explicitly.
Annotate Endpoint With Additional Information
In the file MyController.hpp we add
ENDPOINT_INFO above the root
ENDPOINT with summary and response information:
Build, Run, and go to http://localhost:8000/swagger/ui in the browser. Refresh
Notice, that summary is added to the endpoint and MyDto schema automatically documented in the Models.
Expand endpoint info and check that response is documented correctly
Basically that’s it!!!
Now we have Swagger-UI integrated into our project and we can easily add and document endpoints!
Let’s add one more “echo” endpoint and see how it is documented in the swagger.
In the file MyController.hpp:
Build and run… Refresh…
Expand echo endpoint info — you can see the “status” and “Request Body” parameters:
Put in some values:
Execute the request and verify that you have a correct response from the server:
That’s it. You may experiment by adding more endpoints, playing with parameters, and DTO-fields to see how it is being documented in Swagger-UI.