Implementing API Documentation
1. Implementing OpenAPI Annotations
Example: springdoc-openapi
@Operation(summary = "Place an order", description = "Creates an order from a cart")
@ApiResponses({
@ApiResponse(responseCode = "201", description = "Created"),
@ApiResponse(responseCode = "400", description = "Validation failed")
})
@PostMapping("/orders")
public OrderResponse place(@Valid @RequestBody CreateOrderRequest req) { ... }
2. Documenting Request/Response Examples
Example: @ExampleObject
@RequestBody(content = @Content(examples = {
@ExampleObject(name = "minimal", value = "{\"customerId\":\"c_1\",\"lines\":[]}")
}))
3. Documenting Authentication
Example: Security scheme
@OpenAPIDefinition(security = @SecurityRequirement(name = "bearer"))
@SecurityScheme(name = "bearer", type = SecuritySchemeType.HTTP, scheme = "bearer", bearerFormat = "JWT")
public class OpenApiConfig {}
4. Documenting Schema Definitions
| Annotation | Purpose |
|---|---|
@Schema(description, example) | Field-level docs |
@Schema(required = true) | Marks required |
@Schema(allowableValues) | Enum-like |
@ArraySchema | List schemas |
5. Documenting Error Responses
Example: Common error responses
@ApiResponse(responseCode = "401", description = "Unauthenticated",
content = @Content(schema = @Schema(implementation = ProblemDetail.class)))
@ApiResponse(responseCode = "429", description = "Rate limited",
headers = @Header(name = "Retry-After", schema = @Schema(type = "integer")))
6. Implementing Code Generation Configuration
| Tool | Output |
|---|---|
| openapi-generator | Clients in 50+ languages |
| swagger-codegen | Predecessor, still used |
| orval / openapi-typescript | TS clients |
| Spring Cloud Contract | Stubs for testing |
7. Using Documentation Annotations
| Annotation | Use |
|---|---|
@Tag | Group endpoints |
@Parameter | Param description |
@Hidden | Exclude from spec |
@Deprecated | Marks deprecated DEPRECATED |
8. Implementing Custom Documentation Generators
| Tool | Approach |
|---|---|
| Spring REST Docs | Test-driven docs from passing tests |
| AsciiDoc + Asciidoctor | Lightweight markup → HTML/PDF |
| Custom plugin | Read OpenAPI, render Markdown |
9. Implementing Documentation Versioning
| Practice | Detail |
|---|---|
| Per-version spec | /v1/openapi.json, /v2/openapi.json |
| Changelog | Document differences |
| Spec diff in CI | openapi-diff to detect breaking changes |
10. Implementing Interactive Documentation
| Tool | Detail |
|---|---|
| Swagger UI | Try-it-out forms |
| Redoc | Three-pane layout |
| Stoplight Elements | Modern UI |
| Postman Collections | Importable from OpenAPI |
11. Documenting API Rate Limits
Example: Rate-limit docs
Limit: 1000 requests / minute / API key
Headers returned:
X-RateLimit-Limit: 1000
X-RateLimit-Remaining: 998
X-RateLimit-Reset: 1717000000
On exceed: 429 Too Many Requests with Retry-After (seconds)
12. Documenting Deprecation Notices
| Place | Format |
|---|---|
| OpenAPI | "deprecated": true per operation |
| Response header | Deprecation, Sunset |
| Changelog | Date, replacement, reason |