Quick revision
The short answer
We add springdoc-openapi-starter-webmvc-ui (or the WebFlux variant), and at runtime it scans the application's controllers and serves the OpenAPI document at /v3/api-docs and Swagger UI at /swagger-ui.html. It reads request mappings, parameter annotations, Bean Validation constraints and DTO types to describe each endpoint. We improve the description with annotations from io.swagger.v3.oas.annotations such as @Operation, @ApiResponse, @Schema and @Parameter, group endpoints with GroupedOpenApi beans, and disable the UI in production when it should not be public.
What to remember
In this answer, we added springdoc-openapi to a Spring Boot application and saw that it scans controllers at runtime, reading mappings, parameters, validation constraints and DTO types to serve /v3/api-docs and Swagger UI. We improved an endpoint with @Operation, @ApiResponse, @Schema and @Parameter to show its real statuses, errors and examples, split the API into GroupedOpenApi groups, and disabled or protected the UI in production.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We add springdoc-openapi-starter-webmvc-ui (or the WebFlux variant), and at runtime it scans the application's controllers and serves the OpenAPI document at /v3/api-docs and Swagger UI at /swagger-ui.html. It reads request mappings, parameter annotations, Bean Validation constraints and DTO types to describe each endpoint. We improve the description with annotations from io.swagger.v3.oas.annotations such as @Operation, @ApiResponse, @Schema and @Parameter, group endpoints with GroupedOpenApi beans, and disable the UI in production when it should not be public.
Why this question is asked
An OpenAPI document is the machine-readable description of a REST API: its paths, parameters, request and response bodies and errors. Clients generate SDKs from it, gateways import it and developers read it through Swagger UI. In Spring Boot applications the usual way to produce it is springdoc-openapi, which generates the document from the code we already have. Interviewers ask about it because almost every Spring team uses it, and because the difference between a generated document and a useful one is in the details. In this answer, we will add springdoc, see what it reads from our code, improve an endpoint's description step by step, group the API and secure it for production.
1Adding springdoc to a Spring Boot Application
springdoc-openapi is a library that inspects a running Spring application and builds an OpenAPI 3 document from it. For Spring MVC we add the starter that includes Swagger UI; for WebFlux there is a matching webflux starter. The 2.x line targets Spring Boot 3 and a newer line targets Spring Boot 4, so check the version that matches your Boot release.
Nothing else is required for a first document. That is the appeal of the code-first approach: the description is always generated from the code that actually runs.
2What springdoc Reads From Our Code
When the document is requested, springdoc walks through Spring's registered request mappings. For each handler method it records the path and HTTP method, the parameters (@PathVariable, @RequestParam, @RequestHeader, @RequestBody), and the return type. It turns DTO classes into JSON schemas by inspecting them the way Jackson would serialize them, and it reads Bean Validation constraints so that @NotBlank, @Size(max = 100) or @Min(1) appear as required, maxLength and minimum in the schema.
- Spring request mappingspaths, methods, handler signatures
- Parameter and body analysisannotations, DTO types, validation constraints
- Swagger annotations
@Operation,@ApiResponse,@Schema,@Parameteroverride and add - Customizers and groups
OpenApiCustomizer,GroupedOpenApibeans - Served output/v3/api-docs and Swagger UI
5 layers, from the top down. Step through them one at a time.
Notice the last comment: the generated document says 200 even if the method really returns 201, because springdoc can only see the declared return type, not which status we set at runtime. It also cannot know which errors the method may produce. That is where annotations come in.
3Improving One Endpoint's Description
The annotations come from the swagger-annotations package, io.swagger.v3.oas.annotations, which springdoc already brings in. @Operation gives a summary and description, @ApiResponse documents each status code with its body, @Parameter describes a parameter, and @Schema describes a field or class with examples.
The document now shows the real 201 status, the 422 error with a problem details body, a meaningful description of the header and example values. A client developer reading Swagger UI, or an SDK generator, gets the same information we would otherwise put in a wiki.
4Grouping the API
A large application may expose a public API, an admin API and internal endpoints. Instead of one huge document, we define groups with GroupedOpenApi beans, and each group gets its own document at /v3/api-docs/{group} and its own entry in Swagger UI.
Groups also let us publish only the public document to partners while keeping the admin one internal.
5Production Settings
Swagger UI is convenient in development but in production it may expose internal endpoints and invite casual probing. If the API is not meant to be public, we turn the UI off with springdoc.swagger-ui.enabled=false, and the document itself with springdoc.api-docs.enabled=false, often only in the production profile. Many teams instead keep /v3/api-docs available behind authentication for gateways and tooling, or export the document at build time and publish it to a catalog, so the running service does not need to serve it at all.
6Follow-up questions
How do you document a generic error response once for all endpoints?
There are two common ways. springdoc reads @RestControllerAdvice classes: an @ExceptionHandler method annotated with @ResponseStatus, such as BAD_REQUEST returning ProblemDetail, is added as a response to every operation automatically. For full control we register an OpenApiCustomizer bean that walks every operation in the document and adds, for example, 400 and 500 responses referencing a shared Problem schema in components. Either way the error format is described once and stays consistent.
Why is the generated status 200 when my method returns 201?
springdoc sees the declared return type, not the status set at runtime with ResponseEntity.status(201). Annotating the method with @ResponseStatus(HttpStatus.CREATED) or documenting the response with @ApiResponse(responseCode = "201") fixes the document.
Should I annotate every DTO field with @Schema?
Only where it adds information: a description that the field name does not convey, an example, a format or a constraint that validation annotations do not express. Annotating everything makes the code noisy without improving the document.
7Conclusion
In this answer, we added springdoc-openapi to a Spring Boot application and saw that it scans controllers at runtime, reading mappings, parameters, validation constraints and DTO types to serve /v3/api-docs and Swagger UI. We improved an endpoint with @Operation, @ApiResponse, @Schema and @Parameter to show its real statuses, errors and examples, split the API into GroupedOpenApi groups, and disabled or protected the UI in production.
Open with
With springdoc I add the springdoc-openapi-starter-webmvc-ui dependency, choosing the version line that matches my Spring Boot release, or the webflux starter for reactive apps. At runtime it inspects Spring's request mappings and builds an OpenAPI 3 document, served at /v3/api-docs, with Swagger UI at /swagger-ui.html.
Then, if they want more
It reads the paths and HTTP methods, the parameter annotations, the request and response DTO types, which it turns into schemas the way Jackson serializes them, and Bean Validation constraints like NotBlank or Size, which become required fields and length limits.
What it cannot know is which statuses and errors a method really produces, so I add annotations from io.swagger.v3.oas.annotations: Operation for the summary and description, ApiResponse for each status such as 201 and a 422 with a ProblemDetail body, Parameter for header descriptions, and Schema for field descriptions and examples.
For common errors I use a RestControllerAdvice that springdoc picks up, or an OpenApiCustomizer that adds them to every operation. I split public and admin endpoints with GroupedOpenApi beans, and in production I disable Swagger UI with springdoc.swagger-ui.enabled=false if the API is not public, or put the docs behind authentication.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Boot 3, Spring Boot 4
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Spring Web MVC
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "How does springdoc-openapi generate an OpenAPI document from a Spring Boot application...": https://scrivio-interview-library.netlify.app/spring-boot/rest-api-design/b581c874a7/