。/apt# Mock Server - API接口模拟服务器
基于Spring Boot + Vue 3的API接口模拟服务器,支持自定义接口配置、多项目管理、权限控制和高并发。
./run.sh
此脚本会自动构建前后端,并将前端静态文件集成到后端 JAR 包中,只需一个命令即可启动完整服务。
服务地址:
# 1. 构建项目(包含前后端)
./build-all-in-one.sh
# 2. 运行 JAR 包
java -jar backend/target/mock-server-1.0.0.jar
# 构建脚本
./build.sh
# 后端
cd backend && mvn spring-boot:run
# 前端(新终端)
cd frontend && npm install && npm run dev
服务地址:
详细构建说明请参考 BUILD_README.md
cd docker
docker-compose up -d
开发环境:
adminAdmin@123(首次启动需设置环境变量)生产环境:必须在环境变量中设置强密码
export ADMIN_USERNAME=admin
export ADMIN_PASSWORD=YourStrongP@ssw0rd123
export JWT_SECRET=your-super-secret-jwt-key-at-least-256-bits-long
# HTTP
curl http://localhost:8080/api/mock/{projectCode}/{path}
# WebSocket
const ws = new WebSocket('ws://localhost:8080/api/ws/mock/{projectCode}/{path}')
访问: http://localhost:8080/api/swagger-ui.html
Swagger登录(如需要):
curl -X POST http://localhost:8080/api/auth/swagger-login \
-d "username=admin&password=Admin@123"
mock-server/ ├── backend/ # 后端代码(Spring Boot) │ ├── src/main/java/com/carolcoral/mockserver/ │ │ ├── config/ # 配置类 │ │ ├── controller/ # 控制器 │ │ ├── entity/ # 实体类 │ │ ├── repository/ # 数据访问层 │ │ ├── service/ # 业务逻辑层 │ │ ├── filter/ # JWT过滤器 │ │ └── util/ # 工具类 │ └── pom.xml ├── frontend/ # 前端代码(Vue 3) │ ├── src/ │ │ ├── views/ # 页面 │ │ ├── router/ # 路由 │ │ ├── stores/ # 状态管理 │ │ └── utils/ # 工具函数 │ └── package.json ├── docker/ # Docker配置 │ ├── Dockerfile │ └── docker-compose.yml ├── build.sh # 构建脚本 ├── run.sh # 启动脚本 └── README.md
# JWT配置
jwt:
secret: ${JWT_SECRET:your-secret-key}
expiration: 1800000 # 30分钟
# 管理员配置
admin:
username: ${ADMIN_USERNAME:admin}
password: ${ADMIN_PASSWORD:} # 生产环境必须设置
email: ${ADMIN_EMAIL:admin@mockserver.com}
server: {
proxy: {
'/api': {
target: 'http://localhost:8080',
changeOrigin: true
}
}
}
# 必需
JWT_SECRET=your-super-secret-jwt-key-at-least-256-bits-long
ADMIN_PASSWORD=YourStrongP@ssw0rd123
# 可选
ALLOWED_ORIGINS=http://localhost:5173,http://localhost:3000
SWAGGER_USERNAME=swagger_admin
SWAGGER_PASSWORD=YourSw@ggerP@ss123
根据请求参数返回不同响应:
$.userId == '123' // userId等于123
$.status == 'active' // status等于active
$.amount > 100 // amount大于100
| 状态码 | 权重 | 概率 |
|---|---|---|
| 200 | 80 | 80% |
| 500 | 20 | 20% |
Apache License 2.0
Copyright (c) 2024 carolcoral