Sub topic 1
What OpenAPI Describes and Why Generate It from Code
Prerequisites
Assume you know basic Spring MVC, Maven dependencies, JSON, HTTP status codes, and REST controllers such as @RestController and @GetMapping.
What OpenAPI Describes
OpenAPI is a machine-readable description of an HTTP API. It records paths, operations, parameters, request bodies, response status codes, and data schemas. Tools can use this description to build interactive documentation, client libraries, and validation checks.
springdoc-openapi inspects Spring MVC controllers and model classes at runtime. It converts mappings and annotations into an OpenAPI 3 document. Generating the document from code keeps the documentation close to the implementation, but you must still add descriptions for behavior that Java annotations cannot infer.
flowchart TD
A[Spring MVC controllers and models] --> B[springdoc-openapi inspection]
B --> C[OpenAPI JSON document]
C --> D[Swagger UI]
C --> E[Client and test tooling]Adding springdoc-openapi to a Spring Boot 3 Project
For a Spring Boot 3 MVC application, add the starter to pom.xml. Use a current 2.x release for springdoc.version.
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>${springdoc.version}</version>
</dependency>The starter supplies both document generation and Swagger UI. Restart the application after adding the dependency.
Generated Endpoints and Swagger UI
With no configuration, springdoc exposes the generated JSON at /v3/api-docs. It also exposes Swagger UI, commonly at /swagger-ui.html or through its redirected UI path.
Open the UI in a browser. Expand an operation, select Try it out, enter parameter values, and choose Execute. Swagger UI sends the HTTP request and displays the URL, request data, response status, headers, and response body. This makes the document useful for manual checks, but it does not replace automated API tests.
Changing Paths and Scanning
Use application.yml to change endpoint paths or limit which controllers are included:
springdoc:
api-docs:
path: /v3/api-docs
swagger-ui:
path: /swagger-ui.html
paths-to-match: /api/**Here, only paths beginning with /api/ are documented. Restricting the scan prevents internal or unrelated controllers from appearing accidentally.
Exercise
Which endpoint returns the generated OpenAPI document, and which property limits documented paths?
Show answer
The default JSON endpoint is /v3/api-docs, and springdoc.paths-to-match limits the paths.