Sub topic 13
Hiding internal endpoints and types
This chapter assumes you have springdoc-openapi running in a Spring Boot 3 project, so /v3/api-docs and Swagger UI already work. It also assumes you know @RestController, Spring MVC parameter binding, and the basic @Tag and @Operation annotations from earlier chapters.
springdoc documents every request mapping it finds. That default also exposes health probes, admin tools, and half-finished features. This chapter covers the ways to remove them from the generated document, and when to use each.
Hiding is not securing
Hiding an endpoint only removes it from the OpenAPI description. The endpoint still exists and still answers requests. Anyone who guesses /internal/reindex can call it.
Choosing a mechanism
The mechanisms differ in scope. Pick the narrowest one that does the job.
Hiding a controller or a method with @Hidden
io.swagger.v3.oas.annotations.Hidden works on a class or a method. On a class it removes every operation in the controller. On a method it removes only that operation.
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 BookAdminController {
@GetMapping("/{id}")
public String publicLookup(@org.springframework.web.bind.annotation.PathVariable long id) {
return "book " + id;
}
@Hidden
@PostMapping("/reindex")
public String reindex() {
return "reindex started";
}
}
@Hidden
@RestController
@RequestMapping("/internal")
class InternalController {
@GetMapping("/cache-stats")
public String cacheStats() {
return "ok";
}
}In the generated document, GET /api/books/{id} appears. POST /api/books/reindex and everything under /internal do not.
@Operation(hidden = true) has the same effect as @Hidden on a method. Prefer @Hidden when the method has no other @Operation attributes. Prefer hidden = true when you want the hidden state visible next to a summary that explains why the operation exists.
Hiding by path or package
Annotating many controllers one by one is tedious and easy to forget. Rules in application.yml apply to the whole application:
springdoc:
paths-to-match: /api/**
paths-to-exclude: /api/internal/**, /api/*/debug
packages-to-exclude: com.example.library.adminThe properties behave like this:
paths-to-matchis an allow list. Only matching paths are documented.paths-to-excludeis a deny list. Matching paths are removed.packages-to-scanrestricts scanning to listed packages.packages-to-excluderemoves controllers from the listed packages.
Values are comma-separated and use Ant-style patterns, where * matches within one path segment and ** matches across segments.
An allow list is safer by default. A new controller under an unlisted path stays out of the docs until you add it. A deny list does the reverse, so a new internal controller leaks into the docs until someone notices.
The same filters exist per group. In a GroupedOpenApi bean, use pathsToExclude(...) and packagesToExclude(...):
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class PublicGroupConfig {
@Bean
public GroupedOpenApi publicApi() {
return GroupedOpenApi.builder()
.group("public")
.pathsToMatch("/api/**")
.pathsToExclude("/api/internal/**")
.packagesToExclude("com.example.library.admin")
.build();
}
}Use group-level filters when different audiences need different views of one application. Use global properties when an endpoint should never appear anywhere.
Hiding fields and model types
Internal data often sits in the same classes you return from controllers. Two annotations remove it from the schema.
@Schema(hidden = true) removes a field from the OpenAPI schema. @JsonIgnore removes it from the schema and from the JSON that Jackson reads and writes.
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.v3.oas.annotations.media.Schema;
public class Member {
@Schema(description = "Member name", example = "Ada")
private String name;
@Schema(hidden = true)
private String internalAuditNote;
@JsonIgnore
private String passwordHash;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getInternalAuditNote() { return internalAuditNote; }
public void setInternalAuditNote(String note) { this.internalAuditNote = note; }
public String getPasswordHash() { return passwordHash; }
public void setPasswordHash(String hash) { this.passwordHash = hash; }
}The two annotations differ in what happens at runtime:
| Annotation | Removed from OpenAPI schema | Removed from actual JSON |
|---|---|---|
| `@Schema(hidden = true)` | Yes | No |
| `@JsonIgnore` | Yes | Yes |
With @Schema(hidden = true), the field is still serialized in responses. Your documentation then says the field does not exist while clients receive it. That mismatch is rarely what you want for secrets such as passwordHash. Use @JsonIgnore for anything that must not leave the server.
You can also place @Schema(hidden = true) on a whole class to keep that type out of the generated schemas. This suits helper types that only exist for internal wiring.
Hiding method parameters
Some controller arguments are not part of the HTTP contract. A common example is the authenticated user injected by Spring Security:
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/me")
public class ProfileController {
@GetMapping
public String profile(
@Parameter(hidden = true) @AuthenticationPrincipal UserDetails user) {
return user.getUsername();
}
}Without hidden = true, springdoc may list user as a query parameter or request body, and Swagger UI would show a field no client should fill in. springdoc already ignores common framework types such as HttpServletRequest, HttpServletResponse, and Principal. Custom injected types are the ones you must hide yourself.
If a custom type appears in many signatures, annotating each one is repetitive. springdoc has a global switch for that case:
import org.springdoc.core.utils.SpringDocUtils;
import org.springframework.context.annotation.Configuration;
import jakarta.annotation.PostConstruct;
@Configuration
public class IgnoredTypesConfig {
@PostConstruct
void ignoreInternalTypes() {
SpringDocUtils.getConfig()
.addRequestWrapperToIgnore(CurrentTenant.class);
}
public static class CurrentTenant {
// resolved by a custom HandlerMethodArgumentResolver
}
}Use addRequestWrapperToIgnore for argument types and addResponseTypeToIgnore for return types you never want described.
Checking what you hid
After adding exclusions, confirm the result against the generated document rather than trusting the annotations. Fetch the JSON and inspect its paths:
curl -s http://localhost:8080/v3/api-docs | grep -c "/internal"A count of 0 means no internal path appears. Do the same for field names such as internalAuditNote to verify schema hiding. A later chapter turns this check into an automated test, so a new internal controller cannot leak unnoticed.
Common mistakes
- Hiding a path but forgetting its alternatives.
/api/books/reindexcan be hidden while an older/v1/books/reindexmapping stays visible. - Using
@Schema(hidden = true)for secrets. The field is still sent in responses. Use@JsonIgnoreor a separate response type. - Relying on a deny list. Adding
paths-to-excludefor each new internal route misses the one you forget. Preferpaths-to-match. - Expecting
@Hiddento block calls. It changes the documentation, not the routing. - Hiding a parameter that is actually required. If clients must send a header, hiding it hides it from the docs, and clients will not know to send it.
Exercise
A response class has a field ssn. You annotate it with @Schema(hidden = true) and the field disappears from Swagger UI. A tester still sees ssn in a real response. Why does this happen, and what should you change?
Show answer
@Schema(hidden = true) only removes the field from the OpenAPI description. Jackson still serializes it. Replace it with @JsonIgnore, which removes the field from both the schema and the JSON, or return a separate response type that has no ssn field.
Exercise
Your project documents everything under /api/**, but a new /api/internal/metrics controller should not appear in the public docs. Name two ways to hide it, and say which is more robust against future internal controllers.
Show answer
Add @Hidden to the controller, or add /api/internal/** to springdoc.paths-to-exclude. The path rule is more robust for future controllers, because anything created under /api/internal/ is hidden automatically without remembering to annotate it. Neither approach protects the endpoint at runtime, so it still needs Spring Security rules.