What is Swagger UI?
Swagger UI provides an interactive interface for exploring and testing the Library Management API. It automatically generates documentation from the API’s OpenAPI specification and allows you to execute requests directly from your browser.Swagger UI is available at:
http://localhost:8081/swagger-ui/index.htmlAccessing Swagger UI
Prerequisites
- Ensure the application is running on port 8081
- Open your web browser
- Navigate to the Swagger UI endpoint
URL Endpoints
Swagger UI Interface
OpenAPI Specification
Swagger UI endpoints are publicly accessible and do not require authentication.
Swagger Configuration
The API uses OpenAPI 3.0 specification with the following configuration:API Information
| Property | Value |
|---|---|
| Title | TRAINING APP |
| Description | Application for books |
| Version | 1.0.0 |
| Contact | Juan Esteban Camacho Barrera |
| [email protected] |
Available Servers
Swagger UI allows you to switch between different server environments:DEV SERVER
PROD SERVER
Using Swagger UI
1. Explore Available Endpoints
When you open Swagger UI, you’ll see all available API endpoints organized by categories:- auth-user-controller: Authentication endpoints (login, register)
- book-controller: Book management endpoints
- user-controller: User management endpoints
- HTTP method (GET, POST, PUT, DELETE)
- Endpoint path
- Brief description
- Required authentication
2. Authentication with JWT
Most endpoints require JWT authentication. Here’s how to authenticate in Swagger UI:Step 1: Register or Login
- Expand the auth-user-controller section
- Click on
POST /api/v1/auth/loginorPOST /api/v1/auth/register - Click “Try it out”
- Enter your credentials in the request body:
- Click “Execute”
- Copy the JWT token from the response
Step 2: Authorize Swagger UI
- Click the “Authorize” button (lock icon) at the top of the page
- In the “Security token” dialog, enter:
- Click “Authorize”
- Click “Close”
Token Format: Make sure to include “Bearer ” (with a space) before your token.Example:
Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...Once authorized, all subsequent requests will automatically include the JWT token in the Authorization header.
3. Testing Endpoints
After authentication, you can test any endpoint:Making a Request
- Expand an endpoint by clicking on it
- Click “Try it out” button
- Fill in parameters:
- Path parameters (e.g.,
id) - Query parameters (e.g.,
page,size) - Request body (for POST/PUT requests)
- Path parameters (e.g.,
- Click “Execute”
- View the response below
Response Information
Swagger UI displays comprehensive response information:- Response code: HTTP status (200, 201, 404, etc.)
- Response body: JSON response data
- Response headers: HTTP headers returned
- Request duration: Time taken to execute
4. Understanding Request/Response Schemas
Each endpoint shows detailed schema information:- Request Body Schema: Expected JSON structure for POST/PUT requests
- Response Schema: Structure of successful responses
- Required Fields: Fields marked as required with an asterisk (*)
- Data Types: Type information for each field (string, integer, UUID, etc.)
Swagger UI Features
Endpoint Categories
The API is organized into three main controllers:Authentication
auth-user-controller
- User registration
- User login
- JWT token generation
Books
book-controller
- List all books (paginated)
- Find book by ID
- Search by ISBN
- Create/Update/Delete books
Users
user-controller
- List all users (paginated)
- Find user by ID
- Update/Delete users
- Manage book collections
- Get current user
HTTP Methods
Swagger UI color-codes HTTP methods for easy identification:| Method | Color | Common Use Cases |
|---|---|---|
| GET | Blue | Retrieve resources, list items |
| POST | Green | Create resources, login, add to collection |
| PUT | Orange | Update existing resources |
| DELETE | Red | Remove resources |
Models/Schemas
At the bottom of Swagger UI, you’ll find the Schemas section showing all data models:- AuthLoginRequest: Login credentials
- AuthLoginResponse: Login response with JWT token
- AuthRegisterRequest: Registration data
- BookRequest: Book creation/update data
- BookResponse: Book resource representation
- UserRequest: User creation/update data
- UserResponse: User resource representation
- CustomPageableResponse: Pagination wrapper
Common Use Cases
Example 1: Register and Login
- Navigate to
POST /api/v1/auth/register - Try it out with:
- Execute and receive your JWT token
- Use the token to authorize (see Authentication section above)
Example 2: List All Books
- Ensure you’re authenticated (see Authentication section)
- Navigate to
GET /api/v1/books/findAll - Try it out
- Optionally set parameters:
page: 0 (first page)size: 10 (items per page)title: filter by book titleauthor: filter by authorgender: filter by genre
- Execute to see paginated results
Example 3: Create a New Book
- Ensure you’re authenticated
- Navigate to
POST /api/v1/books/create - Try it out with:
- Execute and receive the created book with its UUID
Example 4: Add Book to User Collection
- Ensure you’re authenticated
- Get your user ID from
GET /api/v1/users/logged - Get a book ID from
GET /api/v1/books/findAll - Navigate to
POST /api/v1/users/{userId}/books/{bookId} - Enter both UUIDs in the path parameters
- Execute to add the book to your collection
Security Features
JWT Token Authentication
Swagger UI is configured with HTTP Bearer authentication:- Scheme Type: HTTP
- Security Scheme: Bearer
- Token Format: JWT (JSON Web Token)
- Header: Authorization
- Location: Header
The API uses JWT tokens for stateless authentication. Tokens are validated on every request to protected endpoints.
Token Expiration
If your token expires during testing:- Login again to get a new token
- Re-authorize Swagger UI with the new token
- Continue testing
Tips and Best Practices
Save Your Token
Keep your JWT token in a safe place during testing sessions. You’ll need it to authorize Swagger UI.
Test in Order
Start with authentication endpoints, then test create operations before read operations.
Check Schemas
Review the schema section to understand required fields and data types before making requests.
Use Filters
Take advantage of query parameters like
title, author, and gender to filter results.Troubleshooting
Common Issues
| Issue | Solution |
|---|---|
| 401 Unauthorized | Ensure you’ve authorized with a valid JWT token |
| 404 Not Found | Verify the resource exists and you’re using the correct UUID |
| 400 Bad Request | Check that all required fields are provided with correct data types |
| Token expired | Login again to get a fresh JWT token |
| Swagger UI not loading | Verify the application is running on port 8081 |
Getting Help
If you encounter issues:- Check the response body for detailed error messages
- Verify your request matches the schema requirements
- Ensure all required fields are provided
- Check that UUIDs are in the correct format
Next Steps
Authentication
Learn about authentication endpoints
Books API
Explore book management endpoints
Users API
Discover user management features