Sub topic 12
Splitting a large API into groups
This chapter assumes you have a working springdoc-openapi setup on Spring Boot 3, with /v3/api-docs and Swagger UI responding. It also assumes you know how @RequestMapping paths and packages map to controllers.
Why one document stops working
By default springdoc builds one OpenAPI document from every controller it finds. That works for a small service. In a larger application it causes problems:
- Swagger UI shows hundreds of operations in a single list.
- Public clients see admin endpoints they cannot call.
- Versioned APIs such as
/api/v1and/api/v2mix together, and schema names can clash. - Different audiences, such as partners, the web frontend and internal operators, want different titles and descriptions.
A group is a named slice of the application with its own OpenAPI document. springdoc builds each group separately and serves it under its own URL.
Defining groups with GroupedOpenApi
Declare one GroupedOpenApi bean per group. Each bean needs a unique group name, and that name becomes the last segment of the document 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")
.displayName("Public API")
.pathsToMatch("/api/**")
.pathsToExclude("/api/internal/**")
.build();
}
@Bean
public GroupedOpenApi adminApi() {
return GroupedOpenApi.builder()
.group("admin")
.displayName("Admin API")
.pathsToMatch("/admin/**")
.build();
}
}After a restart, request each document:
curl -s http://localhost:8080/v3/api-docs/public | head -c 300
curl -s http://localhost:8080/v3/api-docs/admin | head -c 300Swagger UI now shows a "Select a definition" dropdown. It lists the groups by displayName, or by the group name if you did not set one. The displayName method is available in springdoc 2.x. If your version lacks it, upgrade, or rely on the group name.
How a group selects operations
A group filters the full set of discovered operations. These builder methods apply conditions:
| Method | Matches on |
|---|---|
| `pathsToMatch` | Request path patterns, using Ant-style wildcards such as `/api/**` |
| `pathsToExclude` | Paths to remove after matching |
| `packagesToScan` | Controller packages, for example `com.example.library.api.v2` |
| `packagesToExclude` | Packages to leave out |
| `producesToMatch` / `consumesToMatch` | Media types declared on the mapping |
| `headersToMatch` | Headers required by the mapping |
When you set several conditions, an operation must satisfy all of them. A group that sets pathsToMatch("/api/**") and packagesToScan("com.example.v2") contains only the v2 controllers under /api.
Choose path matching when your URLs already encode the split, such as /api/v1/**. Choose package scanning when the split follows your code structure and URLs do not reflect it. Package scanning breaks silently if you move a controller to another package, so a test that checks group contents (covered in a later chapter) is worth having.
Different metadata per group
Every group would otherwise share the title from your single OpenAPI bean. To give a group its own Info, add an OpenApiCustomizer:
import io.swagger.v3.oas.models.info.Info;
import org.springdoc.core.customizers.OpenApiCustomizer;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class VersionedGroupsConfig {
@Bean
public GroupedOpenApi v1Api() {
OpenApiCustomizer v1Info = openApi -> openApi.info(new Info()
.title("Library API v1")
.version("1.0.0")
.description("Deprecated. Use v2."));
return GroupedOpenApi.builder()
.group("v1")
.pathsToMatch("/api/v1/**")
.addOpenApiCustomizer(v1Info)
.build();
}
@Bean
public GroupedOpenApi v2Api() {
OpenApiCustomizer v2Info = openApi -> openApi.info(new Info()
.title("Library API v2")
.version("2.0.0")
.description("Current version."));
return GroupedOpenApi.builder()
.group("v2")
.pathsToMatch("/api/v2/**")
.addOpenApiCustomizer(v2Info)
.build();
}
}An OpenApiCustomizer receives the finished OpenAPI object for that group, so it can change anything in it. Typical uses are adding servers, tags or extensions. The group customizer runs on top of whatever your OpenAPI bean provides, so the group's values win where they overlap.
The builder also has addOperationCustomizer, which runs once per operation, and addOpenApiMethodFilter, which decides by Java method:
GroupedOpenApi readOnly = GroupedOpenApi.builder()
.group("read-only")
.pathsToMatch("/api/**")
.addOpenApiMethodFilter(method ->
method.isAnnotationPresent(org.springframework.web.bind.annotation.GetMapping.class))
.build();This group contains only methods annotated with @GetMapping. The filter sees the controller method, so it can also check custom annotations you define yourself.
Defining groups in properties
You can declare groups in configuration instead of Java. This suits simple path-based splits that operations teams may want to change without a rebuild.
springdoc:
group-configs:
- group: public
display-name: Public API
paths-to-match: /api/**
- group: admin
display-name: Admin API
paths-to-match: /admin/**Properties cover the common filters only. Customizers and method filters need Java. If a GroupedOpenApi bean and a property entry share a group name, you get a conflict at startup or an unclear winner, so keep each group in one place.
Swagger UI behaviour with groups
Swagger UI loads its list of definitions from /v3/api-docs/swagger-config, which springdoc builds from your groups. Two settings control the dropdown:
springdoc:
swagger-ui:
urls-primary-name: public
groups-order: ASCurls-primary-name selects the group shown when the page first opens. groups-order sorts the dropdown by group name. Without them the first group in the list opens first, which depends on bean registration order.
Deep links also work. /swagger-ui/index.html?urls.primaryName=Admin%20API opens a given definition, using the display name.
Edge cases and gotchas
- Global filters. The top-level
springdoc.paths-to-matchproperty from the earlier chapter on configuration applies to the default document. Avoid mixing it with groups. Put the filters in each group so each document's scope is easy to read. - Overlapping groups. An operation can appear in several groups. That is legal, but a shared schema such as
Bookis then described in each document. If the groups customize schemas differently, you may see unexpected differences. - Unmatched endpoints. An endpoint that matches no group does not appear in the group documents. Add a catch-all group or review the groups when you add a controller.
- Grouping is not security. A group only changes what is documented. An
adminoperation still answers requests from anyone the security rules allow. Protect it with Spring Security and, if needed, restrict the docs as described in the production chapter. - Security rules. The matcher
/v3/api-docs/**already covers every group document andswagger-config. You do not need extra rules per group.
Checking your result
A quick check for each group is to count its paths:
curl -s http://localhost:8080/v3/api-docs/public | jq '.paths | keys'If a path you expected is missing, check three things in order. First, does the pattern match the full request path, including the context path handling you use? Second, is the controller in a scanned package? Third, did a pathsToExclude entry remove it?
Exercise
Your application has controllers under /api/v1 and /api/v2. You define two GroupedOpenApi beans named "v1" and "v2" with pathsToMatch. At which URL do you fetch the JSON for v2, and how do you make Swagger UI open v2 first?
Show answer
Fetch it at /v3/api-docs/v2. Set springdoc.swagger-ui.urls-primary-name to v2 (or to its display name if you set one) so Swagger UI selects that definition when the page opens.
Exercise
A teammate says that putting the admin controllers in a separate group means unauthorized users can no longer call them. Is that correct? What should be done instead?
Show answer
No. A group changes only which operations appear in a generated document. The endpoints still respond to any request that Spring Security allows. Protect them with authorization rules, such as role checks on /admin/**, and optionally limit access to the documentation endpoints.