Using @Operation Annotation for API Descriptions in Spring Boot
📖 Scenario: You are building a simple Spring Boot REST API for a bookstore. You want to add clear descriptions to your API endpoints so that users and developers understand what each endpoint does.
🎯 Goal: Learn how to use the @Operation annotation from io.swagger.v3.oas.annotations to add descriptions to your REST controller methods.
📋 What You'll Learn
Create a Spring Boot REST controller class named BookController.
Add a GET endpoint method named getBookById that returns a book by its ID.
Add an @Operation annotation with a description to the getBookById method.
Add a POST endpoint method named addBook to add a new book.
Add an @Operation annotation with a description to the addBook method.
💡 Why This Matters
🌍 Real World
Adding clear descriptions to API endpoints helps developers and users understand the purpose and usage of each endpoint, improving API usability and documentation quality.
💼 Career
Knowing how to document APIs with annotations like @Operation is valuable for backend developers working with Spring Boot and OpenAPI/Swagger documentation tools.
Progress0 / 4 steps
1
Create the BookController class with a GET endpoint
Create a Spring Boot REST controller class named BookController. Inside it, write a method getBookById that takes a Long id parameter and returns a string "Book with ID: " plus the id. Annotate the method with @GetMapping("/books/{id}") and the class with @RestController.
Spring Boot
Hint
Use @RestController on the class and @GetMapping("/books/{id}") on the method. Use @PathVariable to get the ID from the URL.
2
Add an @Operation annotation with a description to the GET method
Add the @Operation annotation from io.swagger.v3.oas.annotations.Operation above the getBookById method. Set its description attribute to "Get a book by its ID."
Spring Boot
Hint
Import io.swagger.v3.oas.annotations.Operation and add @Operation(description = "Get a book by its ID.") above the method.
3
Add a POST endpoint method to add a new book
Inside the BookController class, add a method named addBook that takes a String bookName parameter annotated with @RequestBody and returns a string "Added book: " plus the bookName. Annotate the method with @PostMapping("/books").
Spring Boot
Hint
Use @PostMapping("/books") and @RequestBody for the parameter.
4
Add an @Operation annotation with a description to the POST method
Add the @Operation annotation above the addBook method. Set its description attribute to "Add a new book to the store."
Spring Boot
Hint
Add @Operation(description = "Add a new book to the store.") above the addBook method.
Practice
(1/5)
1. What is the main purpose of the @Operation annotation in Spring Boot APIs?
easy
A. To add descriptions and summaries to API endpoints for documentation
B. To handle database transactions automatically
C. To secure API endpoints with authentication
D. To configure server port settings
Solution
Step 1: Understand the role of @Operation
The @Operation annotation is used to add descriptive metadata to API endpoints.
Step 2: Identify its main use
It helps generate clear API documentation by providing summaries and detailed descriptions.
Final Answer:
To add descriptions and summaries to API endpoints for documentation -> Option A
Quick Check:
@Operation adds descriptions [OK]
Hint: Remember: @Operation documents APIs, not config or security [OK]
Common Mistakes:
Confusing @Operation with security annotations
Thinking it manages database or server settings
Using it to handle authentication
2. Which of the following is the correct way to add a short summary and a detailed description using @Operation?
easy
A. @Operation(title = "Short info", details = "Detailed info")
B. @Operation(summary = "Short info", description = "Detailed info")
C. @Operation(info = "Short info", text = "Detailed info")
D. @Operation(header = "Short info", body = "Detailed info")
Solution
Step 1: Recall the correct attribute names
The @Operation annotation uses summary for short info and description for detailed info.
Step 2: Match attributes to options
Only @Operation(summary = "Short info", description = "Detailed info") uses summary and description correctly.
D. The description value must be a string, not a number
Solution
Step 1: Check attribute types in @Operation
The description attribute must be a string, but 12345 is a number.
Step 2: Verify other parts
The summary is present, @PostMapping is correct, and return type can be Item.
Final Answer:
The description value must be a string, not a number -> Option D
Quick Check:
@Operation attributes require string values [OK]
Hint: Always use quotes for @Operation text values [OK]
Common Mistakes:
Passing non-string values to summary or description
Confusing HTTP method annotations
Expecting void return type for create methods
5. You want to document an API endpoint that returns a list of products with a short summary and a detailed description including usage notes. Which @Operation annotation usage is best?
hard
A. @Operation(description = "Returns all products.")
B. @Operation(summary = "List products")
C. @Operation(summary = "List products", description = "Returns all products. Use filters to narrow results.")
D. @Operation(notes = "Returns all products with filters.")
Solution
Step 1: Identify required documentation details
You need both a short summary and a detailed description with usage notes.
Step 2: Match attributes to requirements
@Operation(summary = "List products", description = "Returns all products. Use filters to narrow results.") uses both summary and description properly to provide clear info.
Step 3: Check other options
Options B and C provide only one attribute; D uses an invalid attribute notes.
Final Answer:
@Operation(summary = "List products", description = "Returns all products. Use filters to narrow results.") -> Option C
Quick Check:
Use both summary and description for full docs [OK]
Hint: Use both summary and description for complete API docs [OK]
Common Mistakes:
Using only summary or description alone
Using unsupported attributes like notes
Leaving out usage details in description
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.