<!--DEBUG:--><!--DEBUG:dc3-united-states-software-in-english-pdf-2--><!--DEBUG:--><!--DEBUG:dc3-united-states-software-in-english-pdf-2--><!--DEBUG-spv-->{"id":1684894,"date":"2020-07-31T23:40:00","date_gmt":"2020-07-31T21:40:00","guid":{"rendered":"http:\/\/nhub.news\/?p=1684894"},"modified":"2020-08-01T05:04:52","modified_gmt":"2020-08-01T03:04:52","slug":"c-restful-web-service-with-swagger-ui-and-auto-documented-endpoints","status":"publish","type":"post","link":"http:\/\/nhub.news\/de\/2020\/07\/c-restful-web-service-with-swagger-ui-and-auto-documented-endpoints\/","title":{"rendered":"C++ RESTful Web Service With Swagger-UI and Auto-Documented Endpoints"},"content":{"rendered":"<p style=\"text-align: justify;\"><b>A 5 minutes tutorial how-to-use C++ web framework Oat++ to build RESTful web-service and integrate it with Swagger-UI.<\/b><br \/>\nLet&#8217;s be friends: Comment (0) Join the DZone community and get the full member experience. 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. Clone oatpp repo: On Mac\/Linux: For more detailed instructions see &#8211; installing Oat++ on Unix\/Linux. On Windows: For more detailed instructions see &#8211; 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. Clone \u201coatpp-starter\u201d Build \u201coatpp-starter\u201d on Mac\/Linux Build \u201coatpp-starter\u201d on Windows Run compiled executable: Now go to localhost:8000 and make sure that you can see the response from server MyController class &#8211; contains declared endpoints and their info together with additional Swagger annotations. MyDto class &#8211; resides in DTOs.hpp. Describes the Data-Transfer-Object used in the &#8222;Hello World&#8220; response mentioned above. In oatpp DTOs are used in ObjectMapping and Serialization\/Deserialization. App.cpp file &#8211; this is an applications&#8216; entry point. Here Application Components are loaded, Controllers&#8216; endpoints are added to the Router, and the server starts. AppComponent class &#8211; 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: Install oatpp-swagger Linux\/Mac: Windows: Add oatpp-swagger to CMakeLists.txt Here we add oatpp::swagger::DocumentInfo and 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 Our endpoint is already present in the document with the proper method and path. Oat++ automatically documents most of the endpoints\u2019 info, such as endpoint name, method, path, parameter names, and parameter types. However, there are things that should be specified explicitly. 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\u2019s it \ufffd\ufffd Now we have Swagger-UI integrated into our project and we can easily add and document endpoints! Let\u2019s add one more &#8222;echo&#8220; endpoint and see how it is documented in the swagger. In the file MyController.hpp: Build and run\u2026 Refresh\u2026 Expand echo endpoint info &#8211; you can see the &#8222;status&#8220; and &#8222;Request Body&#8220; parameters: Put in some values: Execute the request and verify that you have a correct response from the server: That\u2019s it. You may experiment by adding more endpoints, playing with parameters, and DTO-fields to see how it is being documented in Swagger-UI. DZone Article DZone Article Free DZone Refcard Comment (0) Published at DZone with permission of Leonid St. See the original article here. Opinions expressed by DZone contributors are their own.<\/p>\n<script>jQuery(function(){jQuery(\".vc_icon_element-icon\").css(\"top\", \"0px\");});<\/script><script>jQuery(function(){jQuery(\"#td_post_ranks\").css(\"height\", \"10px\");});<\/script><script>jQuery(function(){jQuery(\".td-post-content\").find(\"p\").find(\"img\").hide();});<\/script>","protected":false},"excerpt":{"rendered":"<p>A 5 minutes tutorial how-to-use C++ web framework Oat++ to build RESTful web-service and integrate it with Swagger-UI. Let&#8217;s be friends: Comment (0) Join the DZone community and get the full member experience. 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. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1684893,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[93],"tags":[],"_links":{"self":[{"href":"http:\/\/nhub.news\/de\/wp-json\/wp\/v2\/posts\/1684894"}],"collection":[{"href":"http:\/\/nhub.news\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/nhub.news\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/nhub.news\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/nhub.news\/de\/wp-json\/wp\/v2\/comments?post=1684894"}],"version-history":[{"count":1,"href":"http:\/\/nhub.news\/de\/wp-json\/wp\/v2\/posts\/1684894\/revisions"}],"predecessor-version":[{"id":1684895,"href":"http:\/\/nhub.news\/de\/wp-json\/wp\/v2\/posts\/1684894\/revisions\/1684895"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/nhub.news\/de\/wp-json\/wp\/v2\/media\/1684893"}],"wp:attachment":[{"href":"http:\/\/nhub.news\/de\/wp-json\/wp\/v2\/media?parent=1684894"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/nhub.news\/de\/wp-json\/wp\/v2\/categories?post=1684894"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/nhub.news\/de\/wp-json\/wp\/v2\/tags?post=1684894"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}