This repository demonstrates how to automatically create a Web API specification document from source code and how to generate an API client application (in various languages) from it.
This part requires Java 8 and Gradle. The OpenAPI Specification Explained
- Add
'org.springdoc:springdoc-openapi-ui:1.6.9'
to generate OpenAPI UI in the browser - Add
'io.swagger.core.v3:swagger-core:2.2.2'
to include code annotations for documenting API endpoints - Document the API endpoints using code annotations (see GreetingController.java).
- Run the web app
gradle bootRun
and navigate tolocalhost:8080/swagger-ui/index.html
to see the interactive documentation. - Access the OpenAPI specification JSON document on
http://localhost:8080/v3/api-docs
To generate the OpenAPI spec as a file, add the springdoc-openapi-gradle-plugin and run:
gradle clean generateOpenApiDocs
The file is located in <path-to-project>/build/openapi.json
. Make sure no other process is running on port :8080
.
The OpenAPI Generator requires node.js. See the instructions for installation and usage.
List the available generators:
openapi-generator-cli list
Validate the OpenApi spec file:
openapi-generator-cli validate -i <path-to>/openapi.json
Generate a C# API Client:
openapi-generator-cli generate -i <path-to>/openapi.json -g csharp-netcore -o ./tmp/ClientApp/