These annotations help describe API inputs and outputs clearly. They make your API easier to understand and document.
@Parameter and @Schema annotations in Spring Boot
@Parameter(name = "paramName", description = "Description here", required = true) @Schema(description = "Field description", example = "exampleValue", required = true)
@Parameter is used on method parameters to describe them.
@Schema is used on model fields or parameters to add details like examples or descriptions.
@Parameter(name = "id", description = "User ID", required = true) public ResponseEntity<User> getUser(@PathVariable String id) { ... }
public class User { @Schema(description = "User's full name", example = "Jane Doe") private String name; }
@Parameter(description = "Page number for pagination", example = "1", required = false) @RequestParam int page
This Spring Boot controller uses @Parameter to describe the path variable 'id'. The User class uses @Schema to add descriptions and examples to its fields. This helps generate clear API docs.
import org.springframework.web.bind.annotation.*; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.media.Schema; import org.springframework.http.ResponseEntity; @RestController @RequestMapping("/users") public class UserController { @GetMapping("/{id}") public ResponseEntity<User> getUser( @Parameter(name = "id", description = "The unique ID of the user", required = true) @PathVariable String id) { User user = new User(id, "Alice Smith"); return ResponseEntity.ok(user); } } class User { @Schema(description = "User identifier", example = "123") private String id; @Schema(description = "Full name of the user", example = "Alice Smith") private String name; public User(String id, String name) { this.id = id; this.name = name; } public String getId() { return id; } public String getName() { return name; } }
Use @Parameter on controller method parameters to describe API inputs.
Use @Schema on model fields to add detailed info like examples and descriptions.
These annotations improve auto-generated API docs and help others understand your API.
@Parameter describes API method parameters.
@Schema describes data model fields with examples and descriptions.
Both help create clear, useful API documentation automatically.
Practice
@Parameter annotation in Spring Boot APIs?Solution
Step 1: Understand the role of @Parameter
The@Parameterannotation is used to add descriptive information about method parameters in API endpoints.Step 2: Differentiate from other annotations
It is not used for database, security, or exception handling but specifically for API documentation clarity.Final Answer:
To describe method parameters for API documentation -> Option AQuick Check:
@Parameter describes API method parameters = D [OK]
- Confusing @Parameter with @Schema for data models
- Thinking @Parameter manages security or exceptions
- Using @Parameter for database field mapping
@Parameter?Solution
Step 1: Recall the correct attribute name
The@Parameterannotation uses the attributedescriptionto add text about the parameter.Step 2: Verify syntax correctness
Only @Parameter(description = "User ID") String userId usesdescriptioncorrectly; others use invalid attribute names.Final Answer:
@Parameter(description = "User ID") String userId -> Option BQuick Check:
@Parameter uses description attribute = A [OK]
- Using 'value' instead of 'description' attribute
- Using non-existent attributes like 'desc' or 'text'
- Omitting the attribute name entirely
@Parameter(description = "User's age", required = true) int age
What effect does the
required = true have in API documentation?Solution
Step 1: Understand the required attribute
Therequired = trueflag indicates that the parameter must be provided by the API caller.Step 2: Effect on API documentation
This makes the parameter appear as mandatory in generated API docs, guiding users to supply it.Final Answer:
Marks the parameter as mandatory in the API docs -> Option AQuick Check:
required=true means mandatory parameter = B [OK]
- Thinking required=true sets default values
- Assuming it hides the parameter
- Confusing required=true with optional parameters
@Schema annotation:@Schema(description = "User's email", required = "true") private String email;
Solution
Step 1: Check attribute types in @Schema
Therequiredattribute expects a boolean value, not a string.Step 2: Identify the error in the code
Usingrequired = "true"is incorrect; it should berequired = truewithout quotes.Final Answer:
The required attribute should be a boolean, not a string -> Option CQuick Check:
@Schema required must be boolean, not string = C [OK]
- Putting boolean values in quotes
- Assuming @Schema only works on methods
- Thinking field visibility affects @Schema
@Schema annotation usage is correct?Solution
Step 1: Understand attribute types for @Schema
Attributes likemaxLengthexpect an integer, andnullableexpects a boolean.Step 2: Match requirements with options
The field can be null, sonullable = trueis needed, and maxLength should be 50 as an integer.Step 3: Eliminate incorrect options
@Schema(description = "Optional comment", maxLength = "50", nullable = "true") private String comment; uses strings for boolean and integer, which is wrong. @Schema(description = "Optional comment", maxLength = 50, required = true) private String comment; marks it required (not nullable). @Schema(description = "Optional comment", maxLength = 50, nullable = false) private String comment; sets nullable false, disallowing null.Final Answer:
@Schema(description = "Optional comment", maxLength = 50, nullable = true) private String comment; -> Option DQuick Check:
Use correct types and nullable=true for optional fields = A [OK]
- Using strings instead of boolean or int for attributes
- Confusing nullable and required attributes
- Setting nullable false when field can be null
Ready to prove it?
Pass the test and this pattern moves unknown
