Sub topic 2
Describing a Spring Boot API with OpenAPI Metadata
Assume you know Spring MVC controllers, Maven dependencies, JSON, HTTP status codes, and basic Bean Validation. This chapter shows how to make generated API documentation clearer without changing runtime behavior.
Describe the API with an `OpenAPI` bean
springdoc-openapi detects controllers automatically, but an OpenAPI bean supplies document-level metadata. This metadata appears in the generated JSON and Swagger UI.
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class OpenApiConfig {
@Bean
public OpenAPI libraryOpenApi() {
return new OpenAPI()
.info(new Info()
.title("Library API")
.version("1.0.0")
.description("Manage the book catalog"));
}
}Keep the version meaningful. It identifies the API document, not necessarily the application build version.
Annotate controllers and operations
Use @Tag to group related operations. Use @Operation to explain one endpoint.
@Tag(name = "Books", description = "Operations on the book catalog")
@RestController
@RequestMapping("/api/books")
public class BookController {
@Operation(
summary = "Get a book by ID",
description = "Returns one book, or 404 if it does not exist.")
@GetMapping("/{id}")
public Book getBook(@PathVariable long id) {
return service.findById(id);
}
}A summary should be short enough to scan in Swagger UI. Put behavior, constraints, and important failure cases in the description.
> callout note > An annotation improves the contract shown to users. It does not validate requests or create missing error handling.
Document request and response models
@Schema describes a model or field. Bean Validation annotations such as @NotBlank and @Size can also contribute constraints to the generated schema.
@Schema(description = "A book in the catalog")
public record Book(
@Schema(description = "Unique identifier", example = "42",
accessMode = Schema.AccessMode.READ_ONLY)
Long id,
@NotBlank
@Size(max = 200)
@Schema(description = "Book title", example = "Dune")
String title,
@NotBlank
@Schema(description = "Author name", example = "Frank Herbert")
String author) {
}| Annotation | Documents |
|---|---|
| `@Schema` | A model or field |
| `@NotBlank` | A required, non-empty string |
| `@Size` | Length limits |
Document path, query, and header parameters
Use @Parameter when a parameter needs an explanation or example.
@Operation(summary = "Search books")
@GetMapping
public List<Book> search(
@Parameter(description = "Text in the title", example = "dune")
@RequestParam(required = false) String q,
@Parameter(description = "Client request ID")
@RequestHeader("X-Request-ID") String requestId) {
return service.search(q, requestId);
}For a path variable, place @Parameter beside @PathVariable. Document whether query parameters are optional, what formats they accept, and which headers callers must send.
Exercise
Which annotation supplies document-level title and version metadata?
Show answer
An OpenAPI bean using new OpenAPI().info(new Info()...).