API documentation (OpenAPI) & versioning
springdoc-openapi and Swagger UI, documenting security, versioning strategies (/api/v1 vs headers), and evolving APIs without breaking clients.
OpenAPI
OpenAPI (formerly Swagger) is a machine-readable description of your API: paths, parameters, schemas, auth and responses. springdoc-openapi generates it at runtime from your controllers and DTOs:
/v3/api-docsserves the JSON spec./swagger-ui.htmlserves an interactive UI.
@Tag(name = "Bookings")
@Operation(summary = "Approve a pending booking")
@ApiResponse(responseCode = "409", description = "Room already full")
@PostMapping("/{id}/approve")
public BookingDto approve(@PathVariable UUID id) { /* ... */ }
@Bean
OpenAPI api() {
return new OpenAPI()
.info(new Info().title("Hostel API").version("v1"))
.components(new Components().addSecuritySchemes("bearer",
new SecurityScheme().type(SecurityScheme.Type.HTTP).scheme("bearer").bearerFormat("JWT")));
}
The spec is also useful for generating clients, e.g. a TypeScript SDK for your React frontend with openapi-typescript, and for contract tests.
In your projects
pom.xml (springdoc-openapi 3.0.2) · @Tag on controllersSwagger is permitAll in SecurityConfig so the frontend team can browse it.
@Tag(name = "Carts")springdoc 3.0.2 with tags per controller.
shared/config/OpenApiConfig.java · identity/web/OpenApiHeadersSperoTek documents its custom headers (X-Step-Up-Token, X-Correlation-Id) and serves Swagger UI publicly. Consider protecting or disabling it in production for a private SaaS, because it hands attackers a map of your API.
Versioning
| Strategy | Example | Pros | Cons |
|---|---|---|---|
| URI path (SperoTek) | /api/v1/companies |
Obvious, easy to route and cache | URLs change between versions |
| Header | Accept: application/vnd.spero.v2+json |
Clean URLs | Harder to test and cache |
| Query param | ?version=2 |
Simple | Messy |
| None + evolution | Additive changes only | Simplest | Needs discipline |
SperoTek uses /api/v1/.... The other projects use /api/... with no version, which is fine while you control the only client.
Non-breaking vs breaking changes
- Safe: adding an optional field, a new endpoint, or a new enum value (only if clients tolerate unknown values).
- Breaking: removing or renaming a field, changing a type, making an optional field required, changing a status code.
The robustness principle: be liberal in what you accept and conservative in what you send. Clients should ignore unknown fields. Jackson’s FAIL_ON_UNKNOWN_PROPERTIES=false is Spring Boot’s default.
Mobile clients make this real
IR3D and the Hostel Life mobile app (React Native) mean old app versions stay in the wild for months. You can’t force-update everyone, so breaking changes need a new version or a long deprecation window. Use the Deprecation and Sunset headers to announce them.
Related concepts you must know
- API-first vs code-first. Writing the OpenAPI spec first and generating server stubs is the alternative to springdoc’s code-first approach.
- Contract testing (Pact, Spring Cloud Contract) checks that a consumer and provider agree.
- Changelogs. RM even ships in-app release notes (
release_notes,release_note_readstables).