Sub topic 2
Adding springdoc-openapi to a Spring Boot 3 project
This chapter assumes you can build REST controllers with Spring Boot 3 (@RestController, @GetMapping, @RequestBody), manage a Maven or Gradle dependency, and read JSON and HTTP status codes. You will add one dependency and see what springdoc generates.
What springdoc does
springdoc-openapi is a library that inspects your Spring MVC application when it starts. It reads controllers, mappings, parameter types and return types. From these it builds an OpenAPI 3 description, which is a JSON document that lists your endpoints and the data they accept and return. The same library serves Swagger UI, a web page that reads that document and lets you call the API.
Because the description comes from the code, it changes when the code changes. You do not maintain a separate file by hand.
Choose the right starter
springdoc publishes several artifacts. Pick the one that matches your stack:
| Artifact | Use it for |
|---|---|
| `springdoc-openapi-starter-webmvc-ui` | Spring MVC with Swagger UI |
| `springdoc-openapi-starter-webmvc-api` | Spring MVC, JSON description only |
| `springdoc-openapi-starter-webflux-ui` | Spring WebFlux with Swagger UI |
The 2.x line of springdoc targets Spring Boot 3. Releases from the 1.x line target Spring Boot 2 and use the older javax namespace. They do not work with Spring Boot 3.
Add the dependency
For a Spring MVC application, add this to your pom.xml. Replace ${springdoc.version} with a property set to a current 2.x release.
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>${springdoc.version}</version>
</dependency>No other setup is needed. Spring Boot auto-configuration detects the starter and registers the documentation endpoints.
Run it and check the output
Start the application, then open these two addresses. The examples assume the default port 8080.
http://localhost:8080/v3/api-docsreturns the OpenAPI document as JSON.http://localhost:8080/swagger-ui.htmlredirects to Swagger UI.
Suppose your project has a BookController mapped to /api/books with a GET /{id} method. The JSON then contains a paths entry for /api/books/{id}. It also contains a components section with the schema for the returned Book type. Even with no annotations, springdoc infers the HTTP method, the path variable and the response shape.
Common errors
- 404 on both URLs. The dependency is probably missing from the module you run, or you added the
-apiartifact, which has no UI. Check that the-uistarter is on the classpath. - Startup failures or empty output after upgrading Spring Boot. A 1.x springdoc version is still declared. Use a 2.x release.
- Endpoints missing from the JSON. Only handler methods that Spring MVC maps are included. Confirm the controller is a
@RestControlleror@Controllermethod with a mapping and that it is in a package Spring scans. - 401 or 403 on the URLs. Spring Security is blocking them. A later chapter shows how to allow or restrict these paths.
Exercise
Exercise
You add springdoc-openapi-starter-webmvc-api to a Spring Boot 3 project and open /swagger-ui.html. The page returns 404, but /v3/api-docs works. Why, and what do you change?
Show answer
The -api artifact only serves the JSON description and has no Swagger UI. Replace it with springdoc-openapi-starter-webmvc-ui, which includes both the JSON endpoint and the UI.