Sub topic 1
What OpenAPI describes and why generate it from code
Prerequisites
This chapter assumes you can already:
- Build REST controllers in Spring Boot with
@RestController,@GetMappingand@RequestBody. - Add a dependency to a Maven or Gradle project.
- Read JSON and recognize common HTTP status codes such as 200, 404 and 401.
- Use basic Bean Validation annotations such as
@NotBlank.
What an OpenAPI document describes
OpenAPI is a specification for describing HTTP APIs in a format that both people and programs can read. An OpenAPI document is a JSON or YAML file. It answers these questions about your service:
- Which paths exist, and which HTTP methods does each path accept?
- Which parameters does each operation take (path, query, header), and are they required?
- What does the request body look like, and what does each response body look like?
- Which status codes can an operation return?
- How does a client authenticate?
Here is a small fragment for a GET /api/books/{id} endpoint:
openapi: 3.0.1
info:
title: Library API
version: 1.0.0
paths:
/api/books/{id}:
get:
summary: Get a book by id
parameters:
- name: id
in: path
required: true
schema:
type: integer
format: int64
responses:
"200":
description: Book found
content:
application/json:
schema:
$ref: "#/components/schemas/Book"
"404":
description: Book not found
components:
schemas:
Book:
type: object
properties:
id:
type: integer
format: int64
title:
type: string
author:
type: stringThe paths section lists operations. The components section holds reusable pieces, such as the Book schema, which operations refer to with $ref.
Swagger is the older name for this specification. Today "Swagger" mostly refers to the tools around it, such as Swagger UI, a web page that renders an OpenAPI document and lets you send requests.
Why generate it from code
You can write an OpenAPI file by hand. This is the "contract first" approach. It works well when several teams must agree on an API before anyone writes code. For a service you are building in Spring Boot, a hand-written file has a cost: it drifts. Someone adds a query parameter or renames a field, and nobody updates the file.
Generating the document from code avoids that drift. The controllers are the source of truth, and the document is derived from them.
How springdoc-openapi generates it
springdoc-openapi is a library that builds the OpenAPI document while your application runs. At startup and on request, it does the following:
- It scans Spring MVC mappings such as
@RequestMappingand@GetMappingto find paths and methods. - It reads method signatures to find parameters, request bodies and return types.
- It turns Java types into schemas, including Bean Validation constraints such as
@NotBlankand@Size. - It merges in anything you add with Swagger annotations or an
OpenAPIbean.
With no annotations at all, you still get paths, methods, parameter names and field types. Annotations add what code cannot express alone: summaries, examples, descriptions and documented error responses.
Limits of generating from code
Generated documentation is only as informative as your code. Three things to watch for:
- A method that returns
ObjectorResponseEntity<?>produces a vague schema. - Error responses produced by exception handlers are not visible from the controller method, so you must document them.
- Everything public is documented by default, including endpoints you may not want to expose.
Later chapters show how to handle each of these.
Exercise
Your team renames a field in a Java response class. If the OpenAPI file is generated from code, what happens to the documentation, and why is that better than a hand-written file?
Show answer
The generated document shows the new field name the next time it is produced, because springdoc reads the class structure itself. A hand-written file keeps the old name until someone remembers to edit it, so the documentation and the real API disagree.