Sub topic 3
What you get with no configuration: /v3/api-docs and Swagger UI
This chapter covers what springdoc-openapi produces the moment you add the starter, before you write any annotations or properties. You need a Spring Boot 3 project with the springdoc-openapi-starter-webmvc-ui dependency from the previous chapter, at least one @RestController, and basic familiarity with JSON and HTTP status codes. All examples use Spring MVC, not WebFlux.
The two endpoints that appear
Once the starter is on the classpath and the application starts, springdoc registers its own endpoints alongside your controllers. No code or properties are needed.
| URL | What it returns |
|---|---|
| `/v3/api-docs` | The OpenAPI 3 description as JSON |
| `/v3/api-docs.yaml` | The same description as YAML |
| `/swagger-ui.html` | A redirect to the Swagger UI page |
| `/swagger-ui/index.html` | The Swagger UI page itself |
The JSON document is the real product. Swagger UI is a static web page, bundled in the swagger-ui WebJar that the starter pulls in. When it loads, it fetches /v3/api-docs from your application and draws the result. Any other tool that reads OpenAPI, such as a client code generator or a contract checker, can use the same URL.
A small application to inspect
The following controller is complete enough to run. It needs only spring-boot-starter-web and the springdoc starter.
import java.util.List;
import java.util.Map;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;
@RestController
@RequestMapping("/api/books")
public class BookController {
public record Book(Long id, String title, String author) {
}
private final Map<Long, Book> books = Map.of(
42L, new Book(42L, "Dune", "Frank Herbert"),
43L, new Book(43L, "Emma", "Jane Austen"));
@GetMapping
public List<Book> list(@RequestParam(defaultValue = "") String author) {
return books.values().stream()
.filter(b -> b.author().contains(author))
.toList();
}
@GetMapping("/{id}")
public Book getBook(@PathVariable long id) {
Book book = books.get(id);
if (book == null) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND);
}
return book;
}
}Start the application and request the document:
curl http://localhost:8080/v3/api-docsThe output is a single line of JSON. Reformatted and shortened, the part for getBook looks like this:
{
"openapi": "3.0.1",
"info": { "title": "OpenAPI definition", "version": "v0" },
"servers": [
{ "url": "http://localhost:8080", "description": "Generated server url" }
],
"paths": {
"/api/books/{id}": {
"get": {
"tags": ["book-controller"],
"operationId": "getBook",
"parameters": [
{
"name": "id",
"in": "path",
"required": true,
"schema": { "type": "integer", "format": "int64" }
}
],
"responses": {
"200": {
"description": "OK",
"content": {
"*/*": { "schema": { "$ref": "#/components/schemas/Book" } }
}
}
}
}
}
},
"components": {
"schemas": {
"Book": {
"type": "object",
"properties": {
"id": { "type": "integer", "format": "int64" },
"title": { "type": "string" },
"author": { "type": "string" }
}
}
}
}
}Where each part comes from
springdoc builds this by reading the Spring MVC handler mappings at runtime, plus reflection on the method signatures. Each field in the output has a specific source.
- Paths and methods. The class-level
@RequestMappingand the method-level@GetMappingare combined into/api/books/{id}with thegetmethod. - Tags. With no
@Tag, the controller class name is converted to kebab case, soBookControllerbecomesbook-controller. Swagger UI groups operations by tag. - Operation IDs. The Java method name becomes
operationId. If two methods share a name, springdoc adds a numeric suffix such aslist_1. - Parameters.
@PathVariable,@RequestParamand@RequestHeaderbecome entries inparameters. A path variable is alwaysrequired. A@RequestParamwithdefaultValueis marked not required and gets adefaultin its schema. - Request bodies. A
@RequestBodyargument becomes arequestBodywith a schema for the Java type. - Schemas. Return types and body types become entries under
components/schemas, and operations point to them with$ref. Each type is described once, however many operations use it. - Responses. Every operation gets a
200response, because the generator cannot know which other statuses you return. Avoidmethod gets200with no content. - Content type. The
*/*key appears because the mapping does not declareproduces. Addingproduces = MediaType.APPLICATION_JSON_VALUEto the mapping narrows it toapplication/json. - Info block. The title
OpenAPI definitionand versionv0are placeholders. Replacing them is the job of the OpenAPI bean, covered in a later chapter. - Server URL. The server entry is derived from the incoming request, so it follows the host and port that the caller used.
Notice what is missing. The 404 thrown from getBook does not appear, because springdoc does not analyze method bodies. Documenting it is the subject of the chapter on responses and errors.
What gets included and what does not
By default springdoc documents every @RestController and every @Controller method that has a mapping annotation, in any package the application scans. These are excluded:
- Spring Boot Actuator endpoints. They need the separate
springdoc.show-actuator=trueproperty. - springdoc's own endpoints,
/v3/api-docsand the Swagger UI paths. - Spring Boot's default
/errorhandler. - Methods without a mapping annotation.
Exception handlers in a @RestControllerAdvice class are read as well. If they carry @ResponseStatus, springdoc adds those statuses to the operations of controllers in the same scope. This is the one case where an error status can appear without being declared.
The Swagger UI page
Open http://localhost:8080/swagger-ui.html in a browser. Your browser is redirected to /swagger-ui/index.html, and the page loads the document from /v3/api-docs. You will see the placeholder title, a book-controller section, and one row per operation with its method and path. Expanding a row shows the parameters and the 200 response with its schema. Trying requests from this page is covered in the next chapter.
Common errors with the default setup
| Symptom | Likely cause |
|---|---|
| `/swagger-ui.html` returns 404 | The dependency is `springdoc-openapi-starter-webmvc-api`, which has no UI, or an old `springdoc-openapi-ui` artifact for Spring Boot 2 is in use |
| Application fails at startup with errors about `javax` classes | A Spring Boot 2 era springdoc 1.x library is used with Boot 3, which needs the 2.x starters |
| `/v3/api-docs` returns 401 or 403 | Spring Security is on the classpath and the path is not permitted |
| Swagger UI loads but shows "Failed to load API definition" | The document request failed, for example because of the security rule above, or a context path mismatch |
| A controller is missing from the output | It has no mapping annotation, or `springdoc.paths-to-match` or a package filter excludes it |
| `/v3/api-docs` returns a 500 error | A model class that cannot be introspected, or a conflicting schema name. The log shows the cause |
If your application sets server.servlet.context-path=/shop, every URL moves with it, so the document is at /shop/v3/api-docs. Swagger UI still finds it, because the generated server URL includes the context path.
Checking your own application
A quick routine for any project makes the defaults concrete:
- Start the application and fetch
/v3/api-docs.yaml. - Find one of your controllers under
pathsand confirm the path is the combination of class and method mappings. - Check that each parameter has the right
invalue andrequiredflag. - Look under
components/schemasfor your model types and compare the properties with the Java fields. - Note which statuses are listed. Every operation will show
200only, plus any statuses from@ResponseStatusadvice.
The gaps you find are the work for the following chapters.
Exercise
You add springdoc-openapi-starter-webmvc-ui to a Spring Boot 3 application with a @RestController named OrderController. A method cancel(@PathVariable long id) is mapped with @DeleteMapping("/{id}") under @RequestMapping("/api/orders"), returns void, and throws a 409 ResponseStatusException when the order has shipped. With no other configuration, what will /v3/api-docs show for this operation: its path, tag, operation ID, parameter and responses?
Show answer
The path is /api/orders/{id} with the delete method. The tag is order-controller, and the operation ID is cancel. The single parameter is id, with in set to path, required set to true, and an integer schema with format int64. The only response is 200 with description OK and no content, because the method returns void. The 409 does not appear, because springdoc does not read method bodies. It would show up only if declared with @ApiResponse or an advice class with @ResponseStatus.
Exercise
A teammate reports that /swagger-ui.html returns 404 but /v3/api-docs returns valid JSON. Name the most likely cause and the fix.
Show answer
The project has the API-only artifact springdoc-openapi-starter-webmvc-api, which generates the document but does not bundle Swagger UI. The fix is to switch to springdoc-openapi-starter-webmvc-ui, which includes the API module and the Swagger UI WebJar.