http://localhost:8080{
"code": 200,
"message": "操作成功",
"data": {}
}
200: 操作成功400: 参数验证失败500: 服务器错误POST /api/user/register
请求体:
{
"username": "testuser",
"password": "123456",
"nickname": "测试用户",
"phone": "13800138000",
"email": "test@example.com"
}
响应示例:
{
"code": 200,
"message": "注册成功",
"data": {
"id": 1,
"username": "testuser",
"nickname": "测试用户",
"phone": "13800138000",
"email": "test@example.com",
"isVip": false,
"status": 1
}
}
POST /api/user/login
请求体:
{
"username": "testuser",
"password": "123456"
}
响应示例:
{
"code": 200,
"message": "登录成功",
"data": {
"user": {
"id": 1,
"username": "testuser",
"nickname": "测试用户",
"isVip": false,
"status": 1
},
"token": "mock_token_1"
}
}
GET /api/user/{id}
响应示例:
{
"code": 200,
"message": "操作成功",
"data": {
"id": 1,
"username": "testuser",
"nickname": "测试用户",
"avatar": null,
"phone": "13800138000",
"email": "test@example.com",
"isVip": false,
"status": 1
}
}
GET /api/book/list
请求参数:
| 参数 | 类型 | 必填 | 说明 | 默认值 |
|---|---|---|---|---|
| page | Integer | 否 | 页码 | 1 |
| size | Integer | 否 | 每页数量 | 10 |
| keyword | String | 否 | 搜索关键词(书名、作者、简介) | - |
| categoryId | Integer | 否 | 分类ID | - |
| status | Integer | 否 | 状态(0-下架,1-上架) | - |
| isVip | Boolean | 否 | 是否VIP专享 | - |
响应示例:
{
"code": 200,
"message": "操作成功",
"data": {
"list": [
{
"id": 1,
"title": "西游记",
"author": "(明) 吴承恩",
"cover": "https://picsum.photos/seed/book1/200/300",
"brief": "《西游记》是中国古代第一部神魔题材的长篇章回小说...",
"price": 0.00,
"isFree": true,
"isVip": false,
"categoryId": 1,
"status": 1,
"viewCount": 0,
"likeCount": 0,
"readCount": 0
}
],
"total": 100,
"page": 1,
"size": 10,
"totalPages": 10
}
}
GET /api/book/{id}
响应示例:
{
"code": 200,
"message": "操作成功",
"data": {
"id": 1,
"title": "西游记",
"author": "(明) 吴承恩",
"cover": "https://picsum.photos/seed/book1/200/300",
"image": "https://picsum.photos/seed/book1/200/300",
"brief": "《西游记》是中国古代第一部神魔题材的长篇章回小说...",
"desc": "中国古代第一部浪漫主义章回体长篇神魔小说...",
"introduction": "本书《西游记》是中国古代第一部浪漫主义章回体长篇神魔小说...",
"price": 0.00,
"isFree": true,
"isVip": false,
"categoryId": 1,
"status": 1,
"viewCount": 0,
"likeCount": 0,
"readCount": 0
}
}
POST /api/book
请求体:
{
"title": "新书标题",
"author": "作者名称",
"cover": "https://example.com/cover.jpg",
"image": "https://example.com/image.jpg",
"brief": "简短简介",
"desc": "描述",
"introduction": "详细介绍",
"price": 29.90,
"isFree": false,
"isVip": false,
"categoryId": 1,
"status": 1
}
响应示例:
{
"code": 200,
"message": "创建成功",
"data": {
"id": 10,
"title": "新书标题",
"author": "作者名称",
"status": 1
}
}
PUT /api/book/{id}
请求体: 同创建书籍
响应示例:
{
"code": 200,
"message": "更新成功",
"data": {
"id": 1,
"title": "更新后的书名",
"status": 1
}
}
DELETE /api/book/{id}
响应示例:
{
"code": 200,
"message": "删除成功",
"data": null
}
DELETE /api/book/batch
请求体:
[1, 2, 3]
响应示例:
{
"code": 200,
"message": "批量删除成功",
"data": null
}
# 用户注册
curl -X POST "http://localhost:8080/api/user/register" \
-H "Content-Type: application/json" \
-d '{
"username": "testuser",
"password": "123456",
"nickname": "测试用户"
}'
# 用户登录
curl -X POST "http://localhost:8080/api/user/login" \
-H "Content-Type: application/json" \
-d '{
"username": "testuser",
"password": "123456"
}'
# 查询书籍列表
curl -X GET "http://localhost:8080/api/book/list?page=1&size=10"
# 查询书籍详情
curl -X GET "http://localhost:8080/api/book/1"
# 创建书籍
curl -X POST "http://localhost:8080/api/book" \
-H "Content-Type: application/json" \
-d '{
"title": "新书",
"author": "作者",
"status": 1
}'
// 用户注册
fetch('http://localhost:8080/api/user/register', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
username: 'testuser',
password: '123456',
nickname: '测试用户'
})
})
.then(response => response.json())
.then(data => console.log(data));
// 用户登录
fetch('http://localhost:8080/api/user/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
username: 'testuser',
password: '123456'
})
})
.then(response => response.json())
.then(data => console.log(data));
// 查询书籍列表
fetch('http://localhost:8080/api/book/list?page=1&size=10')
.then(response => response.json())
.then(data => console.log(data));
Content-Type: application/json 头执行 book/src/main/resources/db/schema.sql 文件创建数据库和表结构。
确保 application.properties 中的数据库配置正确:
spring.datasource.url=jdbc:mysql://localhost:3306/books_db?...
spring.datasource.username=root
spring.datasource.password=root