Sub topic 13
Hiding internal endpoints and types
This chapter assumes you have springdoc-openapi running in a Spring Boot 3 project and that you know @RestController and @RequestMapping. It also assumes you have seen the generated /v3/api-docs document. By default springdoc documents every Spring MVC endpoint it finds. Some of those are not meant for API consumers, such as maintenance operations, health helpers, or fields used only inside your service. This chapter shows four ways to keep them out of the specification.
Hide one method or controller with @Hidden
@Hidden removes the annotated element from the generated document. Put it on a method to hide one operation, or on the class to hide the whole controller.
import io.swagger.v3.oas.annotations.Hidden;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/books")
public class BookMaintenanceController {
@GetMapping
public String list() {
return "visible";
}
@Hidden
@PostMapping("/reindex")
public String reindex() {
return "internal only";
}
}The /api/books/reindex endpoint still works at runtime. It no longer appears in /v3/api-docs or Swagger UI. @Operation(hidden = true) has the same effect for a single method.
Hide by path or package with configuration
Annotations require editing each controller. When a whole area should be hidden, use properties instead.
springdoc:
paths-to-exclude: /api/internal/**
packages-to-exclude: com.example.library.internalpaths-to-exclude matches request paths. packages-to-exclude skips controllers in the listed packages. You can also use the inverse, paths-to-match or packages-to-scan, to list only what should be documented. Exclusion is easier to forget when you add new code, because new endpoints appear by default. An allow-list is safer.
For grouped documents, the builder has the same option:
GroupedOpenApi.builder()
.group("public")
.pathsToMatch("/api/**")
.pathsToExclude("/api/internal/**")
.build();Hide fields, parameters and types
A model can contain fields that should not be documented. Use @Schema(hidden = true) on the field. A parameter that Spring fills in itself, such as a current-user object, can be hidden with @Parameter(hidden = true).
import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.media.Schema;
public record Account(
String name,
@Schema(hidden = true)
String internalNote) {
}
@Hidden
class AuditRecord {
}@Hidden on a class removes that type from the schemas section. To ignore a type everywhere without annotating it, for example a class from a library you cannot edit, register it once at startup:
import org.springdoc.core.utils.SpringDocUtils;
import org.springframework.context.annotation.Configuration;
import jakarta.annotation.PostConstruct;
@Configuration
public class OpenApiIgnoreConfig {
@PostConstruct
void ignoreTypes() {
SpringDocUtils.getConfig().addRequestWrapperToIgnore(java.security.Principal.class);
}
}addRequestWrapperToIgnore stops the type from being treated as a request parameter. addResponseTypeToIgnore does the same for return types.
Choosing a method
| Need | Use |
|---|---|
| One operation or controller | `@Hidden` |
| A whole path area | `springdoc.paths-to-exclude` |
| A whole package | `springdoc.packages-to-exclude` |
| One field or parameter | `@Schema(hidden = true)`, `@Parameter(hidden = true)` |
| A third-party type | `SpringDocUtils` ignore methods |
Common errors
- Hiding a controller but leaving its request model in the schemas. Add
@Hiddento the model, or make sure no visible endpoint references it. - Writing
paths-to-exclude: /internaland expecting it to cover/internal/x. Use/internal/**. - Setting both
paths-to-matchandpaths-to-excludeand forgetting that an endpoint must pass both filters to appear.
Exercise
A controller under /api/admin must still work but must not appear in Swagger UI. Name two ways to do this without deleting code, and say what else you must do to keep it safe.
Show answer
Put @Hidden on the controller class, or set springdoc.paths-to-exclude to /api/admin/**. Hiding only removes it from the documentation, so you must also protect the endpoints with Spring Security.