When an API implementation is written separately from its definition, the live response can drift from the published reference. OpenAPI Generator reduces manual translation by producing server stubs, client SDKs, and documentation from an OpenAPI definition used as the source of truth. Change the definition first when the API contract changes, then regenerate the affected artifacts. Generated code still needs application behavior and review. section 6.1 section 6.4
OpenAPI Generator supports multiple languages and frameworks. Its openapi-generator-cli is a Node command-line application that downloads and runs the Java generator executable. For a Spring Boot service, `generate` takes an input OpenAPI file, selects the `spring` generator and `spring-boot` library, and sets an output directory and Java packages. The result includes a Maven project, API interfaces and controllers, model classes, and a copy of the OpenAPI file. The generated controller is a starting point: implement its endpoint behavior and error handling before treating the service as complete. section 6.2.1
Listing 6.1 Generating a Spring Boot application from OpenAPI
openapi-generator-cli generate --input-spec product-catalog-v1-0.oas.yaml #1 --generator-name spring #2 --output product-catalog-service --additional-properties= #3 library=spring-boot, #4 apiPackage=com.acmepetsupplies.controller, #5 modelPackage=com.acmepetsupplies.model, #6 configPackage=com.acmepetsupplies.configuration, #7 basePackage=com.acmepetsupplies, #8 useTags=true #9
The Spring Boot generation command identifies the contract, generator, output directory, library, and package settings; keep the comma-separated additional properties free of spaces between elements. section 6.2.1
Keep regeneration deliberate:
- After changing the OpenAPI definition, regenerate models and stubs. Add a hand-edited controller to `.openapi-generator-ignore` and pass that file with `--ignore-file-override` so regeneration does not overwrite it. Then update the controller to populate newly generated fields. section 6.2.4
- Alternatively, configure `openapi-generator-maven-plugin` to generate during a Maven build. In the shown setup, `mvn clean compile` generates and copies files; invoking `mvn openapi-generator:generate` directly fails. section 6.2.4
- For a client SDK, select the `java` generator and `okhttp-gson` library. The generated API class exposes a `viewProduct(id)` method backed by an HTTP client; configure its base path and authentication before calling it. section 6.2.6
Checking an updated product response
Add `reviewRating` and `numberOfReviews` to the OpenAPI product schema, regenerate, and set both values in the controller. After restarting the service, a request through the API gateway returns the new fields. This checks the running response as well as the generated model; gateway-produced responses also matter when assessing whether the published reference matches observed behavior. section 6.2.4 section 6.4
For repeatable delivery, a GitHub Actions build can run the CLI against a prepared OpenAPI file, generate a Java SDK with a custom template, and store the package in GitHub Packages for API consumers. Templates can also be extracted with `author template`, edited, and supplied through `--template-dir` when bundled output needs adjustment. section 9.3.3 section 6.2.9
