Introduction
Grouping APIs by tags helps organize your endpoints clearly. It makes your API easier to understand and use.
Grouping APIs by tags helps organize your endpoints clearly. It makes your API easier to understand and use.
@Tag(name = "tagName", description = "Description of this group") @RestController @RequestMapping("/api/path") public class YourController { // API methods }
@Tag(name = "Users", description = "Operations about users") @RestController @RequestMapping("/users") public class UserController { // user APIs }
@Tag(name = "Products", description = "Manage products") @RestController @RequestMapping("/products") public class ProductController { // product APIs }
This Spring Boot controller groups its API under the 'Books' tag. When you generate API docs (like Swagger UI), this tag helps organize the endpoint.
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import io.swagger.v3.oas.annotations.tags.Tag; @Tag(name = "Books", description = "Operations related to books") @RestController @RequestMapping("/books") public class BookController { @GetMapping public String listBooks() { return "List of books"; } }
Make sure to add the OpenAPI/Swagger dependency to your project to use @Tag annotations effectively.
Tags improve API documentation but do not affect the API behavior itself.
Use @Tag to group related APIs in your Spring Boot controllers.
Tags help organize and clarify your API documentation.
Each controller can have one or more tags describing its purpose.
@Tag annotation in Spring Boot controllers?@RestController
@Tag(name = "Orders")
public class OrderController {
@GetMapping("/orders")
public List<String> getOrders() { return List.of("order1", "order2"); }
}/orders endpoint?@RestController
@Tag(name = "Products")
public class ProductController {
@GetMapping("/products")
public List<String> getProducts() { return List.of("prod1", "prod2"); }
@Tag(name = "Special")
@GetMapping("/products/special")
public List<String> getSpecialProducts() { return List.of("special1"); }
}@RestController
@Tag(name = "Users")
public class UserController {
@GetMapping("/users")
public List<String> getUsers() { return List.of("user1", "user2"); }
}
@RestController
@Tag(name = "Admins")
public class AdminController {
@GetMapping("/admins")
public List<String> getAdmins() { return List.of("admin1"); }
}Pass the test and this pattern moves unknown