Sub topic 9
Documenting path, query and header parameters
This chapter assumes you have already added the springdoc starter, can open /v3/api-docs and Swagger UI, and have a @RestController such as the BookController from earlier chapters. It also assumes you know @PathVariable, @RequestParam and @RequestHeader.
springdoc already lists most parameters without any annotations. What it can't know is what a parameter means, what a sensible value looks like, and which headers your filters read. This chapter covers what springdoc infers, how to add the rest with @Parameter, and where the generated output goes wrong.
What springdoc infers from the method signature
springdoc reads the Spring MVC annotations and maps each one to an OpenAPI parameter location, written as in in the specification.
| Spring annotation | OpenAPI `in` | Required by default |
|---|---|---|
| `@PathVariable` | `path` | Always true |
| `@RequestParam` | `query` | True, unless `required = false` or `defaultValue` is set |
| `@RequestHeader` | `header` | True, unless `required = false` or `defaultValue` is set |
| `@CookieValue` | `cookie` | True, unless `required = false` or `defaultValue` is set |
The name comes from the annotation's name or value attribute. If you leave it out, springdoc falls back to the Java parameter name. That only works when the class is compiled with -parameters. The Spring Boot parent POM and Gradle plugin enable it, but a custom build may not. Naming the parameter explicitly avoids the problem.
The schema comes from the Java type. int becomes integer with format int32, UUID becomes a string with format uuid, and an enum becomes a string with the list of allowed values. Optional<T> is unwrapped to T and treated as not required. A defaultValue is written to the schema as default.
Adding meaning with @Parameter
io.swagger.v3.oas.annotations.Parameter adds descriptions and examples. The following controller documents a search endpoint with a query parameter, a bounded numeric parameter and an optional header. It assumes the BookService from earlier chapters has a search(String title, int limit) method that returns List<Book>.
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import java.util.List;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/books/search")
@Validated
public class BookSearchController {
private final BookService service;
public BookSearchController(BookService service) {
this.service = service;
}
@Operation(summary = "Search books by title")
@GetMapping
public List<Book> search(
@Parameter(description = "Part of the title, case-insensitive", example = "dune")
@RequestParam(name = "title", required = false) String title,
@Parameter(description = "Maximum number of results")
@RequestParam(name = "limit", defaultValue = "20") @Min(1) @Max(100) int limit,
@Parameter(description = "Correlation id echoed in server logs",
example = "7d1c1f0e-3f1e-4a52-9c1d-0a6f3b2f9e11")
@RequestHeader(name = "X-Request-Id", required = false) String requestId) {
return service.search(title, limit);
}
}Fetch the document and look at the parameters array for this operation:
curl -s http://localhost:8080/v3/api-docs | jq '.paths["/api/books/search"].get.parameters'The limit entry should look roughly like this:
{
"name": "limit",
"in": "query",
"description": "Maximum number of results",
"required": false,
"schema": {
"type": "integer",
"format": "int32",
"default": 20,
"maximum": 100,
"minimum": 1
}
}Three things came from three different sources. The description came from @Parameter. required: false and default: 20 came from defaultValue. minimum and maximum came from the Bean Validation annotations on the parameter.
Useful @Parameter attributes
descriptionandexample: the text and sample value shown in Swagger UI. The example pre-fills the input field.required: overrides what springdoc inferred. Prefer fixing the Spring annotation, so documentation and behavior stay in step.deprecated: marks the parameter as deprecated. Swagger UI shows it struck through.hidden: removes the parameter from the document. Chapter 13 covers hiding in more detail.schema: an@Schemafor the cases where the Java type isn't enough, for example@Schema(allowableValues = {"asc", "desc"})on aStringsort direction.in: the location. It matters mainly for parameters that are not in the method signature.
Parameters that are not in the method signature
A filter or interceptor may read a header that no controller method declares, such as a tenant identifier. springdoc cannot see it, so declare it on the operation:
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Schema;
@Operation(
summary = "List books for a tenant",
parameters = @Parameter(
name = "X-Tenant-Id",
in = ParameterIn.HEADER,
required = true,
description = "Tenant that owns the catalog",
schema = @Schema(type = "string", example = "acme")))
@GetMapping("/tenant")
public List<Book> listForTenant() {
return service.listForCurrentTenant();
}When the same header applies to every operation, repeating it on each method does not scale. springdoc provides the OperationCustomizer interface for that case. You implement it once as a bean and add the parameter to every operation it receives.
Grouping query parameters with @ParameterObject
Controllers with many filters often bind query parameters into one object. Without help, springdoc may describe that object as a single parameter. Add @ParameterObject from org.springdoc.core.annotations and springdoc expands the fields into separate query parameters. Field-level @Parameter and Bean Validation annotations still apply.
import io.swagger.v3.oas.annotations.Parameter;
import jakarta.validation.constraints.Min;
public record BookFilter(
@Parameter(description = "Author name", example = "Frank Herbert")
String author,
@Parameter(description = "Earliest publication year")
@Min(1450)
Integer fromYear) {
}import org.springdoc.core.annotations.ParameterObject;
import org.springframework.data.domain.Pageable;
@GetMapping("/filter")
public List<Book> filter(@ParameterObject BookFilter filter,
@ParameterObject Pageable pageable) {
return service.filter(filter, pageable);
}The second argument shows the most common use. A Spring Data Pageable is expanded into page, size and sort query parameters. Without @ParameterObject, Swagger UI shows a single JSON editor for the pageable, which produces requests that Spring cannot bind.
Common problems
- A parameter appears twice or with the wrong location. This usually means the same name is declared both in the signature and in
@Operation(parameters = ...). Declare it in one place only. - Names show as
arg0,arg1. The-parameterscompiler flag is missing, and you did not give an explicitname. - A path variable is documented as optional. Path variables are always required in OpenAPI. If you map two URLs to one method to make a segment optional, the document describes only one of them.
- A header the client must send is marked optional. If the header is read by a filter, springdoc cannot know it is required. Declare it with
@Parameter(required = true)as shown above. - Swagger UI sends an empty string for a blank optional field. The UI includes empty query parameters unless you untick them. Set
@Parameter(allowEmptyValue = false)for documentation purposes, and make the server treat blank input the same as missing input. - The example in the spec disagrees with the validation.
example = "0"on a field with@Min(1)produces a "Try it out" request that fails. Keep examples inside the constraints.
Exercise
A @RequestParam(defaultValue = "20") int limit is annotated with @Min(1) @Max(100). Which parts of its OpenAPI entry come from the default value and which from the constraints? Also, why might Swagger UI show maximum: 100 while the server still accepts limit=500?
Show answer
The default value makes the parameter required: false and adds default: 20 to the schema. The constraints add minimum: 1 and maximum: 100. springdoc reads the constraints regardless of whether they are enforced, but Spring only validates method parameters when the controller class is annotated with @Validated. Without it, the documentation shows the limit while the server accepts limit=500.