Sub topic 8
Documenting request and response models
This chapter assumes you have springdoc-openapi running in a Spring Boot 3 project, can open /v3/api-docs, and know basic Bean Validation annotations such as @NotBlank. Here you will learn how springdoc turns your Java types into schemas and how to improve them.
How springdoc builds a schema
springdoc inspects the types your controller methods accept and return. For each type it creates an entry under components.schemas in the OpenAPI document. Field names, Java types and nullability come from the class itself. Controllers then refer to the schema by name with $ref, so one model is described once and reused.
The generated schema is correct but bare. It has no descriptions and no examples. The @Schema annotation fills that gap.
Describing a model with @Schema
Put @Schema on the type to describe it as a whole. Put it on each field to describe that field. On a record, place it on the component.
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
@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 = "Title of the book", example = "Dune")
String title,
@NotBlank
@Schema(description = "Author name", example = "Frank Herbert")
String author) {
}The attributes used here are the ones you will reach for most often:
| Attribute | Effect in the document |
|---|---|
| `description` | Text shown next to the field in Swagger UI |
| `example` | Sample value used when Swagger UI builds a request body |
| `accessMode` | `READ_ONLY` fields appear in responses only. `WRITE_ONLY` fields appear in requests only |
| `requiredMode` | Marks the field as required or not required explicitly |
Validation constraints become schema rules
springdoc reads Bean Validation annotations and copies them into the schema. In the Book record, @NotBlank marks title and author as required. @Size(max = 200) adds maxLength: 200 to title. Other constraints work the same way. For example, @Min and @Max become minimum and maximum, and @Pattern becomes pattern.
This is the main benefit of the approach. You write the rule once, the server enforces it, and the documentation states it.
Using the model in a controller
A controller needs no extra annotation to reference the model. Returning Book from a method is enough.
@GetMapping("/{id}")
public Book getBook(@PathVariable long id) {
return service.findById(id);
}The 200 response for this endpoint now points at the Book schema. A method that takes @RequestBody Book book gets the same schema as its request body.
Common errors
- Examples with the wrong type.
exampleis a string in the annotation, but springdoc converts it to the field type. An example such as"abc"on aLongfield produces an invalid sample. - Validation not shown. Constraints appear only when the Bean Validation API is on the classpath. Add
spring-boot-starter-validationif they are missing. - One class for create and read. Reusing
Bookfor both requests and responses forcesidto appear in both. Mark itREAD_ONLY, as above, or use separate request and response types.
Exercise
A Book response must show id, but clients should not be asked to send it when creating a book. Which @Schema setting achieves this, and where do you set it?
Show answer
Set accessMode = Schema.AccessMode.READ_ONLY in the @Schema annotation on the id field (or record component). The field then appears in the response schema and is left out of request bodies in Swagger UI.