Sub topic 9
Documenting path, query and header parameters
This chapter assumes you have a working springdoc setup, as covered in the earlier chapters. It also assumes you know @PathVariable, @RequestParam and @RequestHeader.
What springdoc infers on its own
springdoc reads your Spring MVC method signatures. Each annotated argument becomes an OpenAPI parameter with a location, called in. The type, name and required flag come from the code.
| Spring annotation | OpenAPI `in` | Required flag taken from |
|---|---|---|
| `@PathVariable` | `path` | Always required |
| `@RequestParam` | `query` | `required` and `defaultValue` |
| `@RequestHeader` | `header` | `required` and `defaultValue` |
A defaultValue marks the parameter as optional and appears as the schema default. Without any Swagger annotation, though, the parameter has no description or example. @Parameter adds those.
Adding descriptions and examples
The controller below uses all three parameter kinds. It keeps its data in memory, so it runs as shown.
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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/catalog")
@Tag(name = "Catalog", description = "Search the catalog by shelf")
public class CatalogController {
private static final List<String> TITLES = List.of("Dune", "Dune Messiah", "Emma");
@Operation(summary = "Search titles on a shelf")
@GetMapping("/shelves/{shelf}/titles")
public List<String> search(
@Parameter(description = "Shelf code", example = "SF-1")
@PathVariable String shelf,
@Parameter(description = "Case-insensitive part of the title", example = "dune")
@RequestParam(required = false) String title,
@Parameter(description = "Maximum number of results, 1 to 100", example = "20")
@RequestParam(defaultValue = "20") int size,
@Parameter(description = "Correlation id used in server logs",
example = "7f3c1a9e")
@RequestHeader(name = "X-Request-Id", required = false) String requestId) {
return TITLES.stream()
.filter(t -> title == null || t.toLowerCase().contains(title.toLowerCase()))
.limit(size)
.toList();
}
}Open /v3/api-docs and find the path /api/catalog/shelves/{shelf}/titles. Its parameters array has four entries. In Swagger UI, each one shows its description, and the example value pre-fills the input field.
Restricting values
Bean Validation annotations on parameters also reach the schema. Put @Validated on the controller class, then add constraints such as @Min(1) @Max(100) to size. The generated schema gets minimum and maximum. Swagger UI uses them to describe valid input.
For a fixed set of values, use a Java enum as the parameter type. springdoc lists the constants as enum values, and Swagger UI shows a dropdown.
Hiding and grouping parameters
@Parameter(hidden = true)removes an argument from the documentation. Use it for values the client never sends.- Spring injects
HttpServletRequest,Principaland similar types. springdoc ignores them automatically. - For
Pageableor a class of query fields, annotate the argument with@ParameterObjectfromorg.springdoc.core.annotations. springdoc then expands the fields into separate query parameters. Without it, the object is documented as a request body.
Common errors
- Missing
required = false. Spring then rejects calls without the parameter. The docs match that behavior, so a parameter you meant to be optional appears as required. - Wrong annotation import. Use
io.swagger.v3.oas.annotations.Parameter. The Swagger 1.x and 2.x packages are not read. - Documenting
Authorizationas a header parameter. Swagger UI ignores header parameters namedAuthorization. Declare a security scheme instead, as the security chapter shows. - Examples of the wrong type. The
exampleattribute is a string. springdoc converts it to the parameter type, soexample = "abc"on anintproduces an invalid sample.
Exercise
A query parameter sort has @RequestParam(defaultValue = "title") String sort. Is it shown as required in the generated document, and where would you add its description?
Show answer
It is not required, because a defaultValue makes the parameter optional, and the default "title" appears in its schema. Add the description with @Parameter(description = "...") on the same method argument.