Sub topic 7
Annotating controllers and operations with @Tag and @Operation
This chapter assumes you have springdoc-openapi running in a Spring Boot 3 project, so /v3/api-docs and Swagger UI already respond. It also assumes you can write a @RestController. Without annotations, springdoc groups endpoints under the controller class name and labels each one with the method name. That is accurate but hard to read. Two annotations fix most of it.
What each annotation controls
@Taggoes on a controller class. It sets the group name and description that Swagger UI shows as a section heading.@Operationgoes on a handler method. It sets the summary (a short line next to the path) and the description (longer text shown when the operation is expanded).@ApiResponsesand@ApiResponselist the status codes an operation can return.
All of these live in the io.swagger.v3.oas.annotations packages. They only add text to the generated document. They do not change how Spring handles requests.
A documented controller
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/books")
@Tag(name = "Books", description = "Operations on the book catalog")
public class BookController {
private final BookService service;
public BookController(BookService service) {
this.service = service;
}
@Operation(summary = "Get a book by id", description = "Returns one book, or 404 if it does not exist.")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "Book found"),
@ApiResponse(responseCode = "404", description = "Book not found", content = @Content)
})
@GetMapping("/{id}")
public Book getBook(
@Parameter(description = "Book identifier", example = "42") @PathVariable long id) {
return service.findById(id);
}
}The example assumes a BookService and a Book type exist in your project. Book appears in a later chapter.
Reading the result in Swagger UI:
- The operation appears under a Books heading, with the tag description beside it.
- The row shows
GET /api/books/{id}and the summary "Get a book by id". - Expanding the row shows the description, the
idparameter with its example, and both response codes.
content = @Content on the 404 response means "no body". Without it, springdoc would document the method's return type as the 404 body too.
How the pieces map to the document
Common mistakes
- Mixing up
summaryanddescription. Keep the summary to a short phrase. Put details, rules, and side effects in the description. - Importing the wrong
@Tag. Some IDEs suggestorg.springframework.boot.context.metrics...or other unrelated classes. Useio.swagger.v3.oas.annotations.tags.Tag. - Mixing Swagger 2 annotations.
@Apiand@ApiOperationbelong to the older Springfox library. springdoc ignores them. - Documenting codes that never occur. The annotation text is not checked against your code. If the service never returns 404, the document is wrong, and clients will write handling for a response that never comes.
Exercise
A controller method returns Book, and you add @ApiResponse(responseCode = "404", description = "Not found") without a content attribute. What does Swagger UI show for the 404 response body, and how do you make it show no body?
Show answer
springdoc documents the 404 response with the same Book schema as the return type, which is misleading. Set content = @Content on the annotation to declare an empty body.