Sub topic 2
Adding springdoc-openapi to a Spring Boot 3 project
This chapter adds the springdoc starter to a Spring Boot 3 application and confirms that it works. It assumes you can build a REST controller with @RestController and @GetMapping. It also assumes you can edit a pom.xml or build.gradle and read JSON and HTTP status codes. The later chapters cover the generated endpoints in detail, so this one only checks that the library is active.
What the starter does
springdoc-openapi is a library that builds an OpenAPI 3 description of your application by inspecting it while it runs. You don't write the description by hand. The starter registers Spring Boot auto-configuration, which means Boot wires up the library's beans when it finds the dependency on the classpath. You add no @Enable... annotation and no configuration class.
The working model has four steps:
- The application starts and Spring MVC registers every controller method as a request mapping.
- springdoc's auto-configuration adds two things: a controller that serves the description, and a service that builds it.
- On the first request for the description, springdoc reads the MVC mappings. It reads method signatures, return types,
@RequestBodyand@RequestParamusage, and any Swagger or Bean Validation annotations. - springdoc turns that into an OpenAPI model, serializes it, and caches the result.
Generation happens lazily, on the first request. A mistake in an annotation can therefore stay hidden until someone opens the docs. The final chapter covers an automated test for this.
Choosing the right artifact
springdoc publishes several starters. Pick the one that matches your web stack and whether you want the UI.
| Artifact | Web stack | Includes Swagger UI |
|---|---|---|
| `springdoc-openapi-starter-webmvc-ui` | Spring MVC | Yes |
| `springdoc-openapi-starter-webmvc-api` | Spring MVC | No |
| `springdoc-openapi-starter-webflux-ui` | Spring WebFlux | Yes |
| `springdoc-openapi-starter-webflux-api` | Spring WebFlux | No |
This tutorial uses webmvc-ui. The -api variants produce the JSON description only. They suit services where a separate tool renders the documentation, or where the UI should never ship to production.
Choosing the version
The springdoc major version must match your Spring Boot major version:
- springdoc 1.x targets Spring Boot 2 and the
javax.*packages. Its artifact isspringdoc-openapi-ui. - springdoc 2.x targets Spring Boot 3, Java 17 or later, and the
jakarta.*packages. Its artifacts are thespringdoc-openapi-starter-*family above. - springdoc 3.x is the line for Spring Boot 4. Check the project's release notes if you are on Boot 4.
Within 2.x, the minor version tracks the Spring Framework version. If you see NoSuchMethodError or ClassNotFoundException at startup, upgrade springdoc to the minor release that matches your Boot version. Look up current releases on Maven Central rather than copying a number from an old article.
Adding the dependency
In Maven, define the version as a property and add the starter. Spring Boot's dependency management does not cover springdoc, so the version is required.
<properties>
<springdoc.version>2.8.5</springdoc.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>${springdoc.version}</version>
</dependency>
</dependencies>The version 2.8.5 is only an example of a 2.x release. Replace it with the current release that matches your Boot version.
In Gradle with the Kotlin DSL, the equivalent is:
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springdoc:springdoc-openapi-starter-webmvc-ui:2.8.5")
}The starter pulls in the Swagger annotations, the OpenAPI model classes and the Swagger UI static resources. You get them transitively, so you don't declare them yourself.
A minimal controller to verify with
Without at least one mapping, the generated description has an empty paths section. This controller gives springdoc something to read:
package com.example.library;
import java.util.Map;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/ping")
public class PingController {
@GetMapping
public Map<String, String> ping() {
return Map.of("status", "ok");
}
}Running and verifying
Start the application, then request the description:
./mvnw spring-boot:run
# in a second terminal
curl -s http://localhost:8080/v3/api-docsYou should get compact JSON that begins with {"openapi":"3.0.1" or a similar 3.x version, followed by an info object and a paths object. The paths object contains /api/ping. If that path appears, springdoc is reading your controllers. Open http://localhost:8080/swagger-ui.html in a browser as well. It redirects to the UI page, which lists ping-controller. The next chapter explains both endpoints.
The title in info defaults to OpenAPI definition and the version to v0. A later chapter replaces these with real metadata through an OpenAPI bean.
Common errors
Most setup failures come from one of the causes below.
- Whitelabel 404 on
/swagger-ui.htmlor/v3/api-docs. The usual cause is the wrong artifact, such asspringdoc-openapi-ui1.x on Boot 3. Check the dependency tree for anorg.springdocartifact whose name starts withspringdoc-openapi-starter. - Startup failure mentioning
javax.servletor a missingjakartaclass. A 1.x springdoc is still on the classpath, or a transitive dependency pulls in a library built for Boot 2. Run./mvnw dependency:treeand look for strayspringdoc-openapiartifacts. NoSuchMethodErrorinsideorg.springframework.web. The springdoc minor version is older than your Spring Framework version. Upgrade springdoc.- Docs return 401 or 403, or redirect to a login page. Spring Security is on the classpath and is blocking the paths. Permit them explicitly, as a later chapter shows.
- WebFlux application with the
webmvcstarter. The MVC starter expects servlet components. Use thewebfluxartifacts instead. - Both
webmvcandwebfluxstarters on the classpath. The application type detection can pick the wrong one. Keep only one. - Empty
paths. The controllers are not component-scanned, for example because they sit outside the main application's package. Fix the scan, then reload.
Choosing between the UI and API-only starter
Prefer webmvc-ui while developing. Switching to webmvc-api removes the UI resources and the /swagger-ui.html route, but /v3/api-docs stays available. If you only need to hide the UI in some environments, a property can turn it off without changing the dependency. A later chapter shows that approach. Swapping the artifact is the better choice when you never want the UI code shipped, for example in a hardened container image.
Exercise
You upgrade a service from Spring Boot 2.7 to Spring Boot 3.3. It currently declares org.springdoc:springdoc-openapi-ui:1.7.0, and after the upgrade /swagger-ui.html returns 404 or the application fails at startup. Which artifact and major version should replace it, and why did the old one fail?
Show answer
Replace it with org.springdoc:springdoc-openapi-starter-webmvc-ui at a 2.x version, assuming the service uses Spring MVC. Spring Boot 3 moved from the javax.* packages to jakarta.*. springdoc 1.x is built against javax, so its classes cannot load against Boot 3. springdoc 2.x targets Jakarta and Java 17 or later.
Exercise
You add springdoc-openapi-starter-webmvc-ui and start the application, and curl http://localhost:8080/v3/api-docs returns valid JSON, but the paths object is empty. Name two likely causes.
Show answer
Likely causes are that the application has no controller mappings, or that the controllers are outside the packages Spring Boot component-scans. A package filter that excludes them also gives an empty paths. An @RestController in a package above or beside the main application class is not registered, so springdoc finds nothing to describe.