Sub topic 12
Splitting a large API into groups
This chapter assumes you have springdoc-openapi running in a Spring Boot 3 project, that you can open /v3/api-docs and Swagger UI, and that you know how @RequestMapping paths work. A single OpenAPI document works well for a small service. As the API grows, one long page of unrelated endpoints becomes hard to read. Groups let you split one application into several documents.
What a group is
A group is a named subset of your endpoints with its own OpenAPI document. You define groups with GroupedOpenApi beans. Each bean selects endpoints by path, package, or other rules. Typical splits are public versus admin endpoints, or one group per API version.
springdoc serves each group at /v3/api-docs/{group}. Swagger UI shows a dropdown so readers can switch between groups.
Defining groups
Declare one GroupedOpenApi bean per group. The group name becomes part of the URL.
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class OpenApiGroupsConfig {
@Bean
public GroupedOpenApi publicApi() {
return GroupedOpenApi.builder()
.group("public")
.pathsToMatch("/api/**")
.build();
}
@Bean
public GroupedOpenApi adminApi() {
return GroupedOpenApi.builder()
.group("admin")
.pathsToMatch("/admin/**")
.build();
}
}After you restart the application, these URLs work:
/v3/api-docs/publicreturns only the endpoints under/api./v3/api-docs/adminreturns only the endpoints under/admin./swagger-ui.htmllists both groups in the selector at the top.
Choosing how to select endpoints
pathsToMatch is the most common selector. The builder offers others, and you can combine them.
| Builder method | Selects endpoints by |
|---|---|
| `pathsToMatch` | URL path pattern |
| `pathsToExclude` | URL path pattern to leave out |
| `packagesToScan` | Java package of the controller |
| `packagesToExclude` | Java package to leave out |
Package-based groups suit modular codebases where each feature lives in its own package. Path-based groups suit APIs organised by URL prefix, such as /api/v1 and /api/v2.
Common errors
- A group returns an empty document. The path pattern does not match the real paths. Check that the pattern includes any context path or servlet prefix handling you rely on, and that the controller is actually registered.
- A 404 on
/v3/api-docs/public. The group name in the URL must match thegroup(...)value exactly, including case. - Global properties seem ignored. Once you define
GroupedOpenApibeans, the group settings decide which endpoints appear in each group. A globalspringdoc.paths-to-matchvalue does not replace them. - Metadata is missing from a group. The
OpenAPIbean from the earlier chapter supplies the title and version for the whole application, and each group document starts from it. To give a group its own details, useaddOpenApiCustomizeron the builder.
Exercise
Your application has controllers under /api/v1/** and /api/v2/**. You want a separate Swagger UI entry for each version. What do you define, and at which URL does the v2 document appear if you name that group v2?
Show answer
Define two GroupedOpenApi beans, one with .group("v1").pathsToMatch("/api/v1/**") and one with .group("v2").pathsToMatch("/api/v2/**"). The v2 document appears at /v3/api-docs/v2, and both groups appear in the Swagger UI selector.