Sub topic 5
Changing paths and scanning with springdoc properties
This chapter assumes you have a Spring Boot 3 project with the springdoc starter on the classpath, a few @RestController classes, and that /v3/api-docs and Swagger UI already respond. It also assumes you know how to edit application.yml. You will learn which properties move the endpoints, which ones control what gets scanned, and which mistakes cause empty or missing documentation.
Why properties come before annotations
springdoc works with no configuration. It scans every controller in the application and publishes the result at default paths. That default is often too broad. Actuator endpoints, internal admin controllers and error handlers can all appear in the document. The default URLs may also clash with your own routing.
Properties change the scope of the scan and the location of the endpoints. They do not change how an individual operation is described. Descriptions, summaries and response codes come from annotations, which later chapters cover.
The properties you will use most
All of these live under the springdoc prefix.
| Property | Default | Effect |
|---|---|---|
| `springdoc.api-docs.path` | `/v3/api-docs` | URL of the JSON document |
| `springdoc.api-docs.enabled` | `true` | Turns the JSON endpoint on or off |
| `springdoc.swagger-ui.path` | `/swagger-ui.html` | URL that redirects to Swagger UI |
| `springdoc.swagger-ui.enabled` | `true` | Turns Swagger UI on or off |
| `springdoc.paths-to-match` | `/**` | Only paths matching these patterns are documented |
| `springdoc.paths-to-exclude` | none | Paths matching these patterns are left out |
| `springdoc.packages-to-scan` | none | Only controllers in these packages are documented |
| `springdoc.packages-to-exclude` | none | Controllers in these packages are left out |
Treat the defaults in this table as a guide and confirm them against the springdoc version you use. The property names are stable across the 2.x line.
Moving the endpoints
Here is a configuration that moves both endpoints and limits the scan to paths under /api:
springdoc:
api-docs:
path: /v3/api-docs
swagger-ui:
path: /swagger-ui.html
paths-to-match: /api/**The first two values are the defaults, written out so you can see where they would change. If your application already serves content at /swagger-ui.html, or you want a tidier URL for readers, change them:
springdoc:
api-docs:
path: /docs/openapi
swagger-ui:
path: /docs/uiAfter a restart, the JSON is at /docs/openapi and Swagger UI is reached through /docs/ui. Swagger UI loads the JSON from the configured api-docs path, so changing one path does not break the other. You do not have to update the UI yourself.
Swagger UI itself is served from /swagger-ui/index.html. The swagger-ui.path property sets a URL that redirects there. The static assets stay under /swagger-ui/, and this matters when you secure the endpoints later.
Choosing what to scan
Three families of properties decide which parts of the application appear in the document. They work at different levels.
paths-to-matchandpaths-to-excludefilter by URL pattern.packages-to-scanandpackages-to-excludefilter by Java package of the controller.- Annotations such as
@Hiddenfilter individual methods or classes. Chapter material on hiding covers those.
Filtering by path
paths-to-match takes a list of Ant-style patterns. A request mapping is documented only if it matches at least one pattern. In YAML you can write the value as a comma-separated string or as a list:
springdoc:
paths-to-match:
- /api/**
- /public/**
paths-to-exclude:
- /api/internal/**With this configuration, /api/books/42 is documented, /public/status is documented, and /api/internal/metrics is not. /admin/users is not documented, because it matches no pattern in paths-to-match.
Filtering by package
If your URL scheme does not separate public from internal endpoints, but your package structure does, scan by package:
springdoc:
packages-to-scan: com.example.library.web.api
packages-to-exclude: com.example.library.web.api.legacyControllers in com.example.library.web.api and its subpackages are documented, except those under legacy. A package filter applies to the controller class. It does not look at the types used in method signatures. A model class from a package outside the scan still appears in the schema section if a scanned controller uses it.
Combining filters
When you set several filters, an endpoint must pass all of them. That makes the filters additive restrictions. Setting paths-to-match: /api/** and packages-to-scan: com.example.admin produces an empty document if the admin controllers are mapped under /admin. No single filter is wrong here, but the combination excludes everything.
A worked example
Suppose the library application has these controllers:
| Controller | Package | Mapping |
|---|---|---|
| `BookController` | `com.example.library.web` | `/api/books` |
| `AuthorController` | `com.example.library.web` | `/api/authors` |
| `MaintenanceController` | `com.example.library.web.ops` | `/api/maintenance` |
| `AdminController` | `com.example.library.web.admin` | `/admin/users` |
You want readers to see books and authors only. Two configurations achieve this.
By path:
springdoc:
paths-to-match: /api/**
paths-to-exclude: /api/maintenance/**By package:
springdoc:
packages-to-scan: com.example.library.web
packages-to-exclude: com.example.library.web.ops,com.example.library.web.adminBoth give the same result today. They differ when the code changes. If someone adds ReportController under /api/reports in the web package, both configurations document it automatically. If someone adds a new internal controller under /api/internal in web, the path configuration publishes it unless the exclusion list is updated. Pick the filter that matches the way your team organizes code, and use an exclusion list only for the exceptions.
Verifying the result
Do not trust a configuration until you have read the generated JSON. Start the application and fetch the document:
curl -s http://localhost:8080/v3/api-docs | python3 -m json.tool | head -40To list only the documented paths, use jq:
curl -s http://localhost:8080/v3/api-docs | jq '.paths | keys'The output for the example above should be:
[
"/api/authors",
"/api/authors/{id}",
"/api/books",
"/api/books/{id}"
]If a path you expect is missing, check the filters in the order shown in the diagram. If an unwanted path is present, check whether it matches paths-to-match and whether its controller sits in an excluded package.
Common mistakes
- Using the wrong prefix. Keys such as
springdoc.swagger-ui.pathare correct. Older tutorials usespringfoxorspringdoc.swagger-ui.urlpatterns from other libraries. Spring Boot ignores unknown properties silently, so a typo produces no error and no effect. - Forgetting the context path. If
server.servlet.context-pathis/app, the JSON is at/app/v3/api-docs. Thepaths-to-matchpatterns apply to the mapping without the context path, so write/api/**, not/app/api/**. - Matching too narrowly. The pattern
/api/*matches/api/booksbut not/api/books/42. Use/**for any depth. - Expecting properties to fix security. Moving the documentation to
/docs/uidoes not protect it. Anyone who knows or guesses the URL can read it. Protection is covered in a later chapter. - Editing the wrong profile file. A value in
application-dev.ymlapplies only when thedevprofile is active. If a change seems ignored, check the active profiles.
Properties versus Java configuration
Everything in this chapter can also be expressed in Java with GroupedOpenApi, which lets you set pathsToMatch and packagesToScan per group. Properties are the better choice when you have one document and the rules are simple. They are visible in one file and can differ per profile. Java configuration is the better choice once you need several documents or conditional logic. A later chapter covers it.
A global property and a group definition can coexist. Global filters apply to the default document, and each group applies its own filters. Keep the global rules simple, so you can predict what each document contains.
Exercise
Your application serves /api/books, /api/internal/stats and /admin/users. You set springdoc.paths-to-match: /api/** and springdoc.paths-to-exclude: /api/internal/**. Which paths appear in /v3/api-docs? Then explain why adding springdoc.packages-to-scan: com.example.admin would give an empty document if the books controller lives in com.example.web.
Show answer
Only /api/books appears. /api/internal/stats matches the include pattern but is removed by the exclude pattern, and /admin/users matches no include pattern. Adding packages-to-scan: com.example.admin limits scanning to controllers in that package. The books controller is outside it, and the admin controller is already removed by the path filter, so no endpoint passes every filter and the document has no paths.
Exercise
Write the application.yml that serves the JSON at /docs/openapi, serves Swagger UI through /docs/ui, documents only paths under /api, and skips controllers in the package com.example.library.web.ops.
Show answer
springdoc: api-docs: path: /docs/openapi swagger-ui: path: /docs/ui paths-to-match: /api/** packages-to-exclude: com.example.library.web.ops