API RESTful để quản lý người dùng với đầy đủ tính năng:
- ✅ Phân trang (Pagination)
- ✅ Tìm kiếm (Search)
- ✅ Lọc (Filter)
- ✅ Sắp xếp (Sort)
- ✅ Response tiếng Việt
- ✅ Không trả về thông tin nhạy cảm (password)
Endpoint: GET /api/users
Query Parameters:
| Tham số | Kiểu | Mặc định | Mô tả |
|---|---|---|---|
page |
int | 0 | Số trang (bắt đầu từ 0) |
limit |
int | 10 | Số bản ghi mỗi trang |
search |
string | null | Tìm kiếm theo tên hoặc email |
userType |
string | null | Lọc theo loại: student hoặc staff |
isActive |
boolean | null | Lọc theo trạng thái: true hoặc false |
sortBy |
string | createdAt | Trường sắp xếp |
sortDirection |
string | desc | Hướng sắp xếp: asc hoặc desc |
Ví dụ Request:
# Lấy trang đầu tiên với 10 bản ghi
GET http://localhost:8080/api/users?page=0&limit=10
# Tìm kiếm người dùng có tên chứa "Nguyễn"
GET http://localhost:8080/api/users?search=Nguyễn
# Lọc sinh viên đang hoạt động
GET http://localhost:8080/api/users?userType=student&isActive=true
# Kết hợp nhiều điều kiện
GET http://localhost:8080/api/users?page=0&limit=20&search=Nguyễn&userType=student&isActive=true&sortBy=fullName&sortDirection=ascResponse (Success):
{
"success": true,
"message": "Danh sách người dùng đã được truy xuất thành công",
"data": [
{
"userId": "550e8400-e29b-41d4-a716-446655440000",
"email": "nguyen.van.a@example.com",
"fullName": "Nguyễn Văn A",
"userType": "student",
"phoneNumber": "0123456789",
"isActive": true,
"createdAt": "2025-12-07T10:30:00",
"updatedAt": "2025-12-07T10:30:00"
}
],
"pagination": {
"page": 0,
"limit": 10,
"totalItems": 1,
"totalPages": 1,
"first": true,
"last": true
},
"timestamp": "2025-12-07T10:35:00"
}Response (Không tìm thấy):
{
"success": true,
"message": "Không tìm thấy người dùng phù hợp với điều kiện",
"data": [],
"pagination": {
"page": 0,
"limit": 10,
"totalItems": 0,
"totalPages": 0,
"first": true,
"last": true
},
"timestamp": "2025-12-07T10:35:00"
}Endpoint: GET /api/users/{id}
Ví dụ Request:
GET http://localhost:8080/api/users/550e8400-e29b-41d4-a716-446655440000Response (Success):
{
"success": true,
"message": "Thông tin người dùng đã được truy xuất thành công",
"data": {
"userId": "550e8400-e29b-41d4-a716-446655440000",
"email": "nguyen.van.a@example.com",
"fullName": "Nguyễn Văn A",
"userType": "student",
"phoneNumber": "0123456789",
"isActive": true,
"createdAt": "2025-12-07T10:30:00",
"updatedAt": "2025-12-07T10:30:00"
},
"timestamp": "2025-12-07T10:35:00"
}Response (Lỗi - Không tìm thấy):
{
"success": false,
"message": "Không tìm thấy Người dùng với id: '550e8400-e29b-41d4-a716-446655440000'",
"timestamp": "2025-12-07T10:35:00"
}Endpoint: POST /api/users
Request Body:
{
"email": "nguyen.van.b@example.com",
"fullName": "Nguyễn Văn B",
"passwordHash": "hashed_password_here",
"passwordSalt": "salt_here",
"userType": "student",
"phoneNumber": "0987654321",
"isActive": true
}Response (Success):
{
"success": true,
"message": "Người dùng đã được tạo thành công",
"data": {
"userId": "660e8400-e29b-41d4-a716-446655440001",
"email": "nguyen.van.b@example.com",
"fullName": "Nguyễn Văn B",
"userType": "student",
"phoneNumber": "0987654321",
"isActive": true,
"createdAt": "2025-12-07T10:40:00",
"updatedAt": "2025-12-07T10:40:00"
},
"timestamp": "2025-12-07T10:40:00"
}Response (Lỗi - Email đã tồn tại):
{
"success": false,
"message": "Người dùng đã tồn tại với email: 'nguyen.van.b@example.com'",
"timestamp": "2025-12-07T10:40:00"
}Endpoint: PUT /api/users/{id}
Request Body:
{
"email": "nguyen.van.b.updated@example.com",
"fullName": "Nguyễn Văn B (Đã cập nhật)",
"passwordHash": "new_hashed_password",
"passwordSalt": "new_salt",
"userType": "staff",
"phoneNumber": "0999999999",
"isActive": true
}Response (Success):
{
"success": true,
"message": "Thông tin người dùng đã được cập nhật thành công",
"data": {
"userId": "660e8400-e29b-41d4-a716-446655440001",
"email": "nguyen.van.b.updated@example.com",
"fullName": "Nguyễn Văn B (Đã cập nhật)",
"userType": "staff",
"phoneNumber": "0999999999",
"isActive": true,
"createdAt": "2025-12-07T10:40:00",
"updatedAt": "2025-12-07T10:45:00"
},
"timestamp": "2025-12-07T10:45:00"
}Endpoint: DELETE /api/users/{id}
Ví dụ Request:
DELETE http://localhost:8080/api/users/660e8400-e29b-41d4-a716-446655440001Response (Success):
{
"success": true,
"message": "Người dùng đã được xóa thành công",
"data": null,
"timestamp": "2025-12-07T10:50:00"
}Invoke-RestMethod -Uri "http://localhost:8080/api/users?page=0&limit=10" -Method Get | ConvertTo-Json -Depth 10Invoke-RestMethod -Uri "http://localhost:8080/api/users?search=Nguyễn&userType=student" -Method Get | ConvertTo-Json -Depth 10$body = @{
email = "test@example.com"
fullName = "Người Dùng Test"
passwordHash = "hashed_password"
passwordSalt = "salt"
userType = "student"
phoneNumber = "0123456789"
isActive = $true
} | ConvertTo-Json
Invoke-RestMethod -Uri "http://localhost:8080/api/users" -Method Post -Body $body -ContentType "application/json" | ConvertTo-Json -Depth 10Truy cập Swagger UI để test API trực tiếp:
http://localhost:8080/swagger-ui.html
- Hỗ trợ
pagevàlimit - Trả về thông tin chi tiết:
totalItems,totalPages,first,last
- Tìm kiếm không phân biệt chữ hoa/thường
- Tìm theo tên (
fullName) hoặc email - Sử dụng pattern matching với LIKE
- Lọc theo
userType:studenthoặcstaff - Lọc theo
isActive:truehoặcfalse - Kết hợp nhiều điều kiện lọc
- Sắp xếp theo bất kỳ trường nào:
createdAt,fullName,email, v.v. - Hỗ trợ cả
asc(tăng dần) vàdesc(giảm dần)
- Không trả về
passwordHashvàpasswordSalttrong response - Sử dụng DTO để kiểm soát dữ liệu trả về
- Tất cả message đều bằng tiếng Việt
- Rõ ràng, dễ hiểu cho người dùng Việt Nam
{
"success": boolean,
"message": "string (tiếng Việt)",
"data": object | array | null,
"timestamp": "ISO 8601 datetime"
}{
"success": boolean,
"message": "string (tiếng Việt)",
"data": array,
"pagination": {
"page": int,
"limit": int,
"totalItems": long,
"totalPages": int,
"first": boolean,
"last": boolean
},
"timestamp": "ISO 8601 datetime"
}- Database Connection: Đảm bảo Azure SQL Database đã được cấu hình đúng và có thể kết nối
- Firewall: Thêm IP của bạn vào Azure SQL Server firewall rules
- Validation: Các trường bắt buộc phải có giá trị hợp lệ
- UUID Format: ID phải đúng định dạng UUID
Nếu có vấn đề, vui lòng kiểm tra:
- Logs trong console
- Swagger UI để xem API documentation chi tiết
- Database connection trong
application.properties