Implementing API Documentation
1. Adding Springdoc OpenAPI Dependencies
Example: Springdoc OpenAPI / Swagger UI dependency
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.6.0</version>
</dependency>
2. Accessing Swagger UI
| URL | Purpose |
|---|---|
/swagger-ui.html |
Interactive UI |
/swagger-ui/index.html |
Direct UI URL |
/v3/api-docs |
OpenAPI JSON |
/v3/api-docs.yaml |
OpenAPI YAML |
3. Accessing OpenAPI JSON
Example: Configure Swagger UI path and options
springdoc:
api-docs.path: /v3/api-docs
swagger-ui.path: /swagger-ui.html
swagger-ui.operationsSorter: method
show-actuator: false
paths-to-match: /api/**
4. Customizing API Info (@OpenAPIDefinition)
Example: API info with contact and server definition
@OpenAPIDefinition(
info = @Info(title="Orders API", version="2.1",
description="Order management",
contact=@Contact(name="API Team", email="api@example.com")),
servers = { @Server(url="https://api.example.com") })
@SpringBootApplication
public class App {}
5. Documenting Operations
Example: Document operation with response codes
@Operation(summary="Create order", description="Places a new order")
@ApiResponses({
@ApiResponse(responseCode="201", description="Created"),
@ApiResponse(responseCode="400", description="Invalid input",
content=@Content(schema=@Schema(implementation=ProblemDetail.class)))
})
@PostMapping
public ResponseEntity<OrderDto> create(@RequestBody NewOrder body) { /* … */ }
6. Documenting Parameters
Example: Document path parameter
@GetMapping("/{id}")
public OrderDto get(
@Parameter(description="Order ID", required=true, example="42")
@PathVariable Long id) { /* … */ }
7. Documenting Request/Response Models
Example: Document response model with @Schema
@Schema(description="A customer order")
public record OrderDto(
@Schema(description="Order ID", example="42") Long id,
@Schema(description="Customer email", example="a@b.com") String email,
@Schema(description="Total amount", example="99.99") BigDecimal total
) {}
8. Grouping APIs with Tags
Example: Tag controller and group API paths
@Tag(name="Orders", description="Order operations")
@RestController
public class OrderController { /* … */ }
// Multiple groups
@Bean
GroupedOpenApi publicApi() {
return GroupedOpenApi.builder().group("public").pathsToMatch("/api/public/**").build();
}
9. Adding Security Schemes
Example: Bearer JWT security scheme
@OpenAPIDefinition(
security = @SecurityRequirement(name = "bearerAuth"))
@SecurityScheme(name="bearerAuth", type=SecuritySchemeType.HTTP,
scheme="bearer", bearerFormat="JWT")
@Configuration
public class OpenApiConfig {}
10. Customizing Swagger UI Configuration
| Property | Effect |
|---|---|
springdoc.swagger-ui.tryItOutEnabled |
Enable Try-it-out by default |
springdoc.swagger-ui.filter |
Filter input |
springdoc.swagger-ui.deep-linking |
URL anchors |
springdoc.swagger-ui.persist-authorization |
Save auth across reload |
11. Excluding Endpoints from Documentation
Example: Hide internal endpoints from Swagger
@Hidden
@GetMapping("/internal/debug")
public String internalOnly() { return "…"; }
@Operation(hidden = true)
@PostMapping("/legacy")
public void legacy() {}