|
| 1 | +# ADR-010: Use OpenAPI Swagger Annotations for All REST API Endpoints |
| 2 | + |
| 3 | +Date: 2025-10-24 |
| 4 | + |
| 5 | +## Status |
| 6 | + |
| 7 | +Accepted |
| 8 | + |
| 9 | +## Context |
| 10 | + |
| 11 | +To ensure that the REST API is well-documented and easily understandable for developers and external |
| 12 | +consumers, it is essential to provide clear and standardized documentation. OpenAPI (Swagger) |
| 13 | +annotations allow for the automatic generation of API documentation, which can be visualized using |
| 14 | +tools like Swagger UI. Currently, not all endpoints are annotated, and OpenAPI is not fully enabled |
| 15 | +in the Spring Boot application. |
| 16 | + |
| 17 | +## Decision |
| 18 | + |
| 19 | +All REST API endpoints in the project will be annotated with OpenAPI Swagger annotations. |
| 20 | +Additionally, OpenAPI will be enabled in the Spring Boot application to generate and serve the API |
| 21 | +documentation. |
| 22 | + |
| 23 | +## Consequences |
| 24 | + |
| 25 | +- **Positive**: |
| 26 | + - Provides a standardized and comprehensive API documentation. |
| 27 | + - Simplifies integration for external consumers by offering a clear contract. |
| 28 | + - Reduces manual effort in maintaining API documentation. |
| 29 | + |
| 30 | +- **Negative**: |
| 31 | + - Requires additional effort to annotate all existing endpoints. |
| 32 | + - Developers need to be familiar with OpenAPI annotations. |
| 33 | + |
| 34 | +## Implementation |
| 35 | + |
| 36 | +1. Add the `springdoc-openapi` dependency to the `pom.xml`: |
| 37 | + ```xml |
| 38 | + <dependency> |
| 39 | + <groupId>org.springdoc</groupId> |
| 40 | + <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId> |
| 41 | + <version>2.1.0</version> |
| 42 | + </dependency> |
| 43 | + ``` |
| 44 | + |
| 45 | +2. Annotate all REST API endpoints with OpenAPI annotations: |
| 46 | + ```java |
| 47 | + @RestController |
| 48 | + @RequestMapping("/example") |
| 49 | + @Tag(name = "Example", description = "Example API") |
| 50 | + public class ExampleController { |
| 51 | + |
| 52 | + @Operation(summary = "Get example data", description = "Fetches example data by ID") |
| 53 | + @ApiResponses(value = { |
| 54 | + @ApiResponse(responseCode = "200", description = "Successful operation"), |
| 55 | + @ApiResponse(responseCode = "404", description = "Example not found") |
| 56 | + }) |
| 57 | + @GetMapping("/{id}") |
| 58 | + public ResponseEntity<ExampleDto> getExample(@PathVariable Long id) { |
| 59 | + // Implementation here |
| 60 | + return ResponseEntity.ok(new ExampleDto()); |
| 61 | + } |
| 62 | + } |
| 63 | + ``` |
| 64 | + |
| 65 | +3. Enable OpenAPI in the Spring Boot application: |
| 66 | + - No additional configuration is required as `springdoc-openapi` automatically enables Swagger |
| 67 | + UI at `/swagger-ui.html`. |
| 68 | + |
| 69 | +4. Document this decision in the project's development guidelines to ensure all new endpoints are |
| 70 | + annotated. |
| 71 | + |
| 72 | +5. Verify the generated documentation by accessing the Swagger UI at |
| 73 | + `http://localhost:8080/swagger-ui.html`. |
0 commit comments