How to generate OpenAPI 3.0 YAML file from existing Spring REST API?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
If you already have a Spring REST API, you usually do not need to hand-write the first OpenAPI document from scratch. The typical approach is to add a library that inspects your controllers, request mappings, and model types, then exposes the generated OpenAPI 3 description as JSON or YAML.
Use springdoc-openapi for Existing Spring APIs
For modern Spring Boot applications, springdoc-openapi is the usual choice. Add the starter dependency and let it scan your application at runtime.
For Maven:
For Gradle:
Once the app starts, springdoc generates the OpenAPI document automatically from your existing endpoints.
Fetch the Generated YAML
After you start the application, the generated spec is typically available at:
- '
/v3/api-docsfor JSON' - '
/v3/api-docs.yamlfor YAML'
You can retrieve the YAML directly:
That gives you a real OpenAPI 3 document based on the live Spring configuration, not a manually synced copy that can drift from the code.
A Minimal Spring Example
The generation works best when your controllers already express request and response structure clearly.
From this, springdoc can infer paths, methods, request bodies, and many schema details automatically.
Improve the Generated Spec With Annotations
Automatic generation gets you a solid baseline, but production-quality docs often need extra metadata. Add annotations where the defaults are too vague.
This improves the resulting YAML without forcing you to maintain a separate source of truth for the whole API.
Add Top-Level API Metadata
You can also customize the generated document with an OpenAPI bean:
This helps turn a technically correct specification into one that is actually useful to consumers.
Generate a File as Part of the Build
If you want a checked-in YAML file or an artifact generated in CI, call the docs endpoint during the build or test pipeline after starting the app. Teams often:
- Start the Spring Boot app in CI
- Request
/v3/api-docs.yaml - Save the result as
openapi.yaml - Publish it or compare it in validation steps
That approach keeps the specification synchronized with the running codebase. It also gives downstream tooling a stable artifact for client generation, review, or contract testing without forcing developers to update YAML by hand.
Common Pitfalls
- Expecting perfect docs without annotations can be disappointing when controller signatures do not communicate enough detail.
- Forgetting to include the
springdocdependency means/v3/api-docs.yamlsimply will not exist. - Complex generic wrappers and custom serializers can produce schemas that need manual annotation help.
- Treating a generated YAML file as permanent static documentation leads to drift if it is not refreshed regularly.
Summary
- Add
springdoc-openapito an existing Spring application to generate an OpenAPI 3 specification automatically. - Fetch the YAML from
/v3/api-docs.yaml. - Use annotations and top-level metadata to improve the output.
- In CI, generate the YAML from the running app so the specification stays aligned with the code.
Related reading
- How to generate password_hash for RabbitMQ Management HTTP API
- How to generate Swagger UI from javadocs?
- How to get a custom healthcheck path in a GCE L7 balancer serving a Kubernetes Ingress?
- How to get a Docker container's IP address from the host
- How to generate serial version UID in Intellij
- How to generate TimeUUID in Java/Scala
- How to get a user's time zone?
- How to get all Kubernetes pod IP in each pods?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.