A Spring Boot application for managing user limits. Supports multiple limit types, limit initialization, increase, decrease, occupy, release, and query operations.
- Multi-type Limit Management: Each user can have one limit account per limit type (CREDIT, LOAN, WITHDRAWAL, TRANSFER)
- Limit Operations: Initialize, increase, decrease, occupy, release, and query limits
- Concurrency Control: Pessimistic locking for safe concurrent operations
- Scheduled Tasks: Simulates multi-user operations automatically
- RESTful API: Clean API design with consistent response format
- Java 17
- Spring Boot 3.5.9
- Spring Data JPA
- SQLite Database
- Lombok
- JUnit 5 & MockMvc
src/main/java/com/credit/limit_management/
├── LimitManagementApplication.java # Main application entry
├── controller/
│ └── LimitController.java # REST API endpoints
├── service/
│ ├── LimitService.java # Service interface
│ └── impl/
│ └── LimitServiceImpl.java # Service implementation
├── repository/
│ └── LimitAccountRepository.java # Data access layer
├── entity/
│ ├── LimitAccount.java # Limit account entity
│ └── LimitType.java # Limit type enum
├── dto/
│ ├── LimitRequest.java # Request DTO
│ ├── LimitResponse.java # Response DTO
│ └── ApiResponse.java # Generic API response wrapper
├── exception/
│ ├── GlobalExceptionHandler.java # Global exception handler
│ ├── LimitNotFoundException.java
│ ├── InsufficientLimitException.java
│ ├── DuplicateLimitException.java
│ └── InvalidOperationException.java
└── scheduler/
└── LimitScheduledTask.java # Scheduled task for simulation
- Java 17 or higher
- Maven 3.6 or higher
mvn clean install
mvn spring-boot:run
The application will start on http://localhost:8080
mvn testCreate a new limit account for a user.
POST /api/limits/init
Content-Type: application/json
{
"userId": 1,
"limitType": "CREDIT",
"amount": 10000.0
}
Increase the total limit for a user's limit account.
PUT /api/limits/increase
Content-Type: application/json
{
"userId": 1,
"limitType": "CREDIT",
"amount": 5000.0
}
Decrease the total limit for a user's limit account.
PUT /api/limits/decrease
Content-Type: application/json
{
"userId": 1,
"limitType": "CREDIT",
"amount": 3000.0
}
Occupy (use) a portion of the available limit.
POST /api/limits/occupy
Content-Type: application/json
{
"userId": 1,
"limitType": "CREDIT",
"amount": 2000.0
}
Release (restore) a portion of the used limit.
POST /api/limits/release
Content-Type: application/json
{
"userId": 1,
"limitType": "CREDIT",
"amount": 1000.0
}
Query a specific limit account.
GET /api/limits/{userId}/{limitType}
Example: GET /api/limits/1/CREDIT
Query all limit accounts for a user.
GET /api/limits/{userId}
Example: GET /api/limits/1
All API responses follow a consistent format:
{
"success": true,
"message": "Operation successful",
"data": {
"id": 1,
"userId": 1,
"limitType": "CREDIT",
"totalLimit": 10000.0,
"usedLimit": 2000.0,
"availableLimit": 8000.0,
"createdAt": "2024年01月01日T10:00:00",
"updatedAt": "2024年01月01日T10:30:00"
}
}| Type | Description |
|---|---|
| CREDIT | Credit limit for purchases |
| LOAN | Loan limit for borrowing |
| WITHDRAWAL | Withdrawal limit for cash |
| TRANSFER | Transfer limit for transfers |
The application includes a scheduled task that runs every 10 seconds to simulate multi-user operations:
- Randomly selects a user (ID 1-5)
- Randomly selects a limit type
- Randomly selects an operation (INIT, INCREASE, DECREASE, OCCUPY, RELEASE)
- Randomly generates an amount (100-1000)
Check the console logs to see the scheduled task execution.
- Initialize limit - success and duplicate cases
- Increase limit - success and not found cases
- Decrease limit - success and insufficient limit cases
- Occupy limit - success and insufficient limit cases
- Release limit - success and exceeds used cases
- Query limit - success and not found cases
- All API endpoints tested with MockMvc
- Validation error handling
- Error response testing
The application uses SQLite database stored in limit_management.db file in the project root directory.
CREATE TABLE limit_account ( id INTEGER PRIMARY KEY AUTOINCREMENT, user_id BIGINT NOT NULL, limit_type VARCHAR(20) NOT NULL, total_limit DOUBLE NOT NULL, used_limit DOUBLE NOT NULL, available_limit DOUBLE NOT NULL, created_at TIMESTAMP NOT NULL, updated_at TIMESTAMP NOT NULL, version BIGINT, UNIQUE(user_id, limit_type) );
mvn clean testmvn spring-boot:run
Initialize a limit:
curl -X POST http://localhost:8080/api/limits/init \ -H "Content-Type: application/json" \ -d '{"userId":1,"limitType":"CREDIT","amount":10000}'
Occupy some limit:
curl -X POST http://localhost:8080/api/limits/occupy \ -H "Content-Type: application/json" \ -d '{"userId":1,"limitType":"CREDIT","amount":2000}'
Query limit:
curl -X GET http://localhost:8080/api/limits/1/CREDIT
Release limit:
curl -X POST http://localhost:8080/api/limits/release \ -H "Content-Type: application/json" \ -d '{"userId":1,"limitType":"CREDIT","amount":1000}'
Increase limit:
curl -X PUT http://localhost:8080/api/limits/increase \ -H "Content-Type: application/json" \ -d '{"userId":1,"limitType":"CREDIT","amount":5000}'
Query all limits for user:
curl -X GET http://localhost:8080/api/limits/1
This project is for assessment purposes.