Sub topic 3
What you get with no configuration: /v3/api-docs and Swagger UI
This chapter assumes you have a Spring Boot 3 MVC project with the springdoc-openapi-starter-webmvc-ui dependency from the previous chapter, and that you can read JSON and HTTP status codes. Here you will start the application and see what springdoc produces before you add any annotations or properties.
The two default endpoints
Once the starter is on the classpath, springdoc registers its endpoints automatically. You write no configuration class and set no properties.
| Path | 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 web page that downloads that document and draws it as an interactive interface.
A minimal example
Take a controller with no documentation annotations at all.
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.RestController;
@RestController
@RequestMapping("/api/books")
public class BookController {
public record Book(Long id, String title, String author) {
}
@GetMapping("/{id}")
public Book getBook(@PathVariable long id) {
return new Book(id, "Dune", "Frank Herbert");
}
}Start the application and fetch the description:
curl http://localhost:8080/v3/api-docsThe document already lists GET /api/books/{id}. Open http://localhost:8080/swagger-ui.html in a browser and the same operation appears, ready to try.
What springdoc infers
springdoc reads the Spring MVC handler mappings and the Java signatures. It does not read comments or guess intent.
- Paths and methods come from
@RequestMapping,@GetMappingand the related annotations. - Parameters come from
@PathVariable,@RequestParamand@RequestHeader. The Java type becomes the schema type, solongbecomes an integer with formatint64. - Request bodies come from
@RequestBodyparameters. - Response schemas come from the method's return type, so
Bookbecomes a schema withid,titleandauthor. - Tags group operations by controller. The default tag name is derived from the class name, so
BookControllerbecomesbook-controller. - Operation ids default to the method name, here
getBook.
The generated document has no summaries or descriptions, and it does not document error responses you raise yourself. The next chapters add those.
When the endpoints do not appear
Most failures in this step have one of a few causes.
- 404 on
/v3/api-docs. Check that the dependency is aspringdoc-openapi-starter-*artifact for Spring Boot 3. The olderspringdoc-openapi-uiartifact targets Spring Boot 2. - 401 or 403. Spring Security is protecting the documentation paths. The production chapter covers how to permit them.
- A context path. If
server.servlet.context-pathis/app, the endpoints move to/app/v3/api-docs. - An empty
pathsobject. Your controllers may not be scanned. Aspringdoc.paths-to-matchsetting or a missing@RestControlleris the usual cause.
Exercise
Your application has a context path of /shop. At which URL do you fetch the OpenAPI JSON, and what produces the entries under "paths"?
Show answer
Fetch it at /shop/v3/api-docs. The entries under "paths" come from the Spring MVC handler mappings (@RequestMapping, @GetMapping and the like) that springdoc scans at runtime.