注意事项
- 先检查
application.yml - 测接口
- 确定URL,和HTTP操作
- header是否需要添加参数
- 添加body,是否设置成JSON格式
返回格式
基于MyBatis-plus 封装的
ApiController
Controller层中继承了ApiController类,ApiController是MyBatis-Plus中的类,封装了返回格式
Success
当在Controller层中不能非常明显地发现请求错误时,调用
success()方法,交给Service层去判断返回情况
JSON格式
{
"code": 0, // 0: success, -1: fail
"data": T // T为任意数据类型
"msg": "执行成功" // "执行成功", "操作失败"
}
写法
public R<?> xxxx(xxx, xxx) {
return success(T); // T 为任意数据类型
}
源码
protected <T> R<T> success(T data) {
return R.ok(data);
}
// SUCCESS/FAILED(code, msg)
public static <T> R<T> ok(T data) {
ApiErrorCode aec = ApiErrorCode.SUCCESS; // SUCCESS(0L, "执行成功")
if (data instanceof Boolean && Boolean.FALSE.equals(data)) {
aec = ApiErrorCode.FAILED; // FAILED(-1L, "操作失败")
}
return restResult(data, aec);
}
public static <T> R<T> restResult(T data, IErrorCode errorCode) {
return restResult(data, errorCode.getCode(), errorCode.getMsg());
}
private static <T> R<T> restResult(T data, long code, String msg) {
R<T> apiResult = new R();
apiResult.setCode(code);
apiResult.setData(data);
apiResult.setMsg(msg);
return apiResult;
}
// R<T> 返回格式
public String toString() {
return "R(code=" + this.getCode() + ", data=" + this.getData() + ", msg=" + this.getMsg() + ")";
}
Fail
当在Controller层中非常明显地发现错误,直接调用
failed()方法返回错误信息
JSON格式
{
"code": -1,
"data": null,
"msg": "xxxx" // 传入的String值
}
写法
public R<?> xxx(xxx, xxx) {
return failed("xxxxx");
}
源码
// 只需使用这个方法
protected <T> R<T> failed(String msg) {
return R.failed(msg);
}
protected <T> R<T> failed(IErrorCode errorCode) {
return R.failed(errorCode);
}
public static <T> R<T> failed(String msg) {
return restResult((Object)null, ApiErrorCode.FAILED.getCode(), msg);
}
public static <T> R<T> failed(IErrorCode errorCode) {
return restResult((Object)null, errorCode);
}
public static <T> R<T> restResult(T data, IErrorCode errorCode) {
return restResult(data, errorCode.getCode(), errorCode.getMsg());
}
private static <T> R<T> restResult(T data, long code, String msg) {
R<T> apiResult = new R();
apiResult.setCode(code);
apiResult.setData(data);
apiResult.setMsg(msg);
return apiResult;
}
// R<T> 返回格式
public String toString() {
return "R(code=" + this.getCode() + ", data=" + this.getData() + ", msg=" + this.getMsg() + ")";
}
User
登录
POST /login
Request
{
"account": "test001",
"password": "test001"
}
Response
成功
/*
long userId;
return success(userId);
*/
{
"code": 0,
"data": 1, // user id
"msg": "执行成功"
}
失败
/*
return failed("用户名与密码不匹配");
*/
{
"code": -1,
"data": null,
"msg": "用户名与密码不匹配"
}
说明
此时不涉及Token,直接返回user id
使用Token后,需将 user id 封装成Token后响应返回
拿到账号和密码后,执行 UserService 中 login 方法,判断返回值是否为-1,不是返回success(userId), 是返回failed("用户名与密码不匹配")
UserServiceImpl{
long login(account, password) {
long result = sql
return result;
}
}
SELECT user_id FROM user WHERE account=xxx AND password=xxx;
注册
POST /register
Request
{
"account": "xxx",
"password": "xxx",
"nickname": "xxx", // 注册时,自动生成一个随机字符串
"avatar": "xxx" // 注册时,给一个默认图片地址
}
Response
成功
{
"code": 0,
"data": true,
"msg": "执行成功"
}
失败
{
"code": -1,
"data": null,
"msg": "账号已存在"
}
说明
- 判断账号是否已存在
- 不存在,就插入到数据库,返回
success() - 存在,返回
failed()
获取用户信息
GET /user/{user_id}
Request
url中有user_id
Response
{
"code": 0,
"data": {
// User类
},
"msg": "执行成功"
}
说明
更新用户信息
PUT /user
Request
{
// User类
}
Response
{
"code":
"data": true / false
"msg":
}
说明
Pet
添加宠物
POST /pet
Request
{
// Pet 类
}
Response
{
"code": ,
"data": true/ false,
"msg":
}
说明
需要修改user中的pet_number
删除宠物
DELETE /pet?idList=xxx
Request
| key | value |
|---|---|
| idList | xxx |
Response
{
"code": ,
"data": true / false,
"msg":
}
说明
需要修改user中的pet_number
获取宠物列表
GET /pet
Request
在header中会传入user_id
需获取header中user_id的值
Response
{
"code": 0,
"data": [
{},
{},
...
{}
// pet类集合
]
"msg":
}
说明
为了后面使用token,在header中直接传入user_id
更改selectAll方法
- 获取到
header中user_id的值 - 执行相应的
MP代码
获取单个宠物
GET /pet/{pet_id}
Request
url中有petid
Response
{
"code": ,
"data": ,// pet类
"msg":
}
说明
修改宠物信息
PUT /pet
Request
{
// pet类
}
Response
{
"code": ,
"data": true / false,
"msg":
}
说明
Diary
添加日记
POST /diary
Request
{
// diary 类
}
Response
{
"code": ,
"data": true / false,
"msg":
}
说明
需要修改pet中的diary_number
删除日记
DELETE /diary?idList=xxx
Request
| Key | Value |
|---|---|
| idList | xxx |
Response
{
"code": ,
"data": true / false,
"msg":
}
说明
需要修改pet中的diary_number
获取日记列表
GET /diary?pet_id=xx
Request
| Key | Value |
|---|---|
| pet_id | xxx |
Response
{
"code": ,
"data": [
// diary 类
{},
{}
],
"msg":
}
说明
按照update_time排序
获取单个日记
GET /diary/{diary_id}
Request
url中有diary_id
Response
{
"code": ,
"data": {
// diary 类
},
"msg":
}
说明
修改日记
PUT /diary
Request
{
// diary 类
}
Response
{
"code": ,
"data": true / false,
"msg":
}
说明
Weight
- 执行对Weight表的CRUD
- 获取最新体重值
- 更新宠物表的当前体重
添加体重
POST /weight
Request
{
// weight 类
}
Response
{
"code": ,
"data": true / false,
"msg":
}
说明
根据 create_time 查询weight表中最新的体重值,用此值更新 pet 表中的 weight 字段
删除体重
DELETE /weight?idList=xx
Request
| Key | Value |
|---|---|
| idList | xxx |
Response
{
"code": ,
"data": true / false,
"msg":
}
说明
根据 create_time 查询weight表中最新的体重值,用此值更新 pet 表中的 weight 字段
获取体重列表
GET /weight?pet_id=xxx
Request
| Key | Value |
|---|---|
| pet_id | xxx |
Response
{
"code": ,
"data": [
// Weight 类
{},
{}
],
"msg":
}
说明
体重列表按创建时间排序
获取单个体重
GET /weight/{weight_id}
Request
url中获取weight_id
Response
{
"code": ,
"data": {
// Weight 类
},
"msg":
}
说明
修改体重
PUT /weight
Request
{
// Weight 类
}
Response
{
"code": ,
"data": true / false,
"msg":
}
说明
根据 create_time 查询weight表中最新的体重值,用此值更新 pet 表中的 weight 字段
Cost
- 执行相应SQL操作
- 更新pet表的总消费
添加消费信息
POST /cost
Request
{
// cost 类
}
Response
{
"code": ,
"data": true / false,
"msg":
}
说明
更新 pet 表中的 total_cost
删除消费信息
DELETE /cost?idList=xxx
Request
| Key | Value |
|---|---|
| idList | xxx |
Response
{
"code": ,
"data": true / false,
"msg":
}
说明
更新 pet 表中的 total_cost
获取消费信息列表
GET /cost?pet_id=xxx
Request
| Key | Value |
|---|---|
| pet_id | xxx |
Response
{
"code": ,
"data": [
// cost 类
{},
{}
],
"msg":
}
说明
消费信息列表按创建时间排序
获取单个消费信息
GET /cost/{cost_id}
Request
url中获取cost_id
Response
{
"code": ,
"data": {
// cost类
},
"msg":
}
说明
修改消费信息
PUT /cost
Request
{
// cost 类
}
Response
{
"code": ,
"data": true / false,
"msg":
}
说明
更新pet表中的total_cost
Photo
添加相片
POST /photo
Request
{
// photo 类
}
Response
{
"code": ,
"data": true / false,
"msg":
}
说明
更新pet表中的photo_number
删除相片
DELETE /photo?idList=xxx
Request
| Key | Value |
|---|---|
| idList | xx |
Response
{
"code": ,
"data": true / false,
"msg":
}
说明
更新pet表中的photo_number
获取相片列表
GET /photo?pet_id=xxx
Request
| Key | Value |
|---|---|
| pet_id | xxx |
Response
{
"code": ,
"data": [
// photo 类
{},
{}
],
"msg":
}
说明
获取单个相片
GET /photo/{photo_id}
Request
url中获取photo_id
Response
{
"code": ,
"data": {
// photo 类
},
"msg":
}
说明
Instant
社区界面会显示动态的发布者昵称,头像,发布时间,发布内容,点赞数量,评论数量
具体评论点击动态查看详细内容
添加个人动态
POST /instant
Request
{
// instant 类
}
Response
{
"code": ,
"data": true / false,
"msg":
}
说明
更新user表中的instant_number
删除个人动态
DELETE /instant?idList=xxx
Request
| Key | Value |
|---|---|
| idList | xxx |
Response
{
"code": ,
"data": true / false,
"msg":
}
说明
更新user表中的instant_number
根据idList删除此动态下对应的评论
获取动态
获取动态需要知道当前用户是否已经对该动态点赞
instant表中并不会记录此状态要根据当前发送该请求的用户去判断是否对该动态点赞
需要创建
InstantDTO类,该类有instant(Instant实例化对象)和status属性(0:未点赞,1:已点赞),同时在InstantDTO类中创建有参构造函数在
InstantService中,创建List<InstantDTO> getInstantDTOById(long user_id)方法。每次从数据库中获取到
instant对象后,都转成instantDTO,再返回给controller层
1. 获取推荐动态列表
GET /instant/hot
Request
none
Response
{
"code": ,
"data": [
// InstantDTO对象
{},
{}
],
"msg":
}
说明
按照时间获取
从instant表中按照create_time获取
2. 获取关注的人的动态列表
GET /instant/follow
Request
根据Header拿到用户id
Response
{
"code": ,
"data": [
// InstantDTO 对象
{},
{}
],
"msg":
}
说明
动态列表按时间排序
根据用户ID查询关注的人的ID,根据关注的人的ID获取它们总的动态列表
Comment
添加评论
POST /comment
Request
{
// comment 类
}
Response
{
"code": ,
"data": true / false,
"msg":
}
说明
更新对应此评论的动态中comment_number值
获取评论
评论中要显示评论者的头像,昵称,评论内容和时间
被评论者只需显示昵称
创建
CommentInfoDTO, 包括以下属性
- userAvatar
- userNickname
- parentNickname // 若只是评论动态,该值为空
- content
- createTime
在
CommentService中定义List<CommentInfoDTO> listCommentInfoDTOById(long instantId)
GET /comment?instant_id=xxx
Request
| Key | Value |
|---|---|
| instant_id | xxx |
Response
{
"code": ,
"data": [
{},
{}
],
"msg":
}
说明
评论按时间排序
Like
点赞 & 取消点赞
POST /like
Request
{
"instant_id": xxx,
"user_id": xxx,
"type": 0 // 0: 点赞,1:取消点赞
}
Response
{
"code": ,
"data": true / false,
"msg":
}
说明
判断type
- 0: 点赞, 插入
- 1: 取消点赞,删除
根据instant_id修改instant表中的like_number
Follow
关注 取关
POST /follow
Request
{
"user_id": xxx,
"followed_id": xxx, // 被关注者ID
"type": 0 // 0: 关注,1: 取关
}
Response
{
"code": ,
"data": true / false,
"msg":
}
说明
判断type值
- 将
user_id与followed_id插入到follow表中,并根据user_id更新此用户的follow_number - 将
user_id与follwoed_id插入到fans表中,此时user_id对应fans表中的fans_id,followed_id对应fans表中的user_id,并根据followed_id更新此用户的fans_number
User扩展
获取关注的人列表
前端点击
关注会进入到关注的人的列表,该列表不显示用户的详细信息只展示头像,昵称,性别和个签
当在列表中点击该用户会进入到此用户的空间界面
此时需要创建
UserInfoDTO,包括user_id,nickname,avatar,gender,introduction属性在
UserService中创建List<UserInfoDTO> listUserInfoDTOBy(long user_id)
- 调用
followDao中的方法获取关注的人的集合
GET /user/{user_id}/follow
Request
从url中获取user_id
Response
{
"code": ,
"data": [
// UserInfoDTO 类
{},
{}
],
"msg":
}
说明
根据用户ID在follow表中查找关注的人的ID,再根据他们的ID去查找,最后赋值到UserInfoDTO中
获取粉丝列表
与获取关注的人列表相似
GET /user/{user_id}/fans
Request
从url中获取user_id
Response
{
"code": ,
"data": [
// UserInfoDTO 类
{},
{}
],
"msg":
}
说明
获取用户空间信息
用户空间中包括昵称,头像,性别,个签,地址,本人动态内容
创建
UserZoneDTO类,包含以上属性在
UserController中添加R<?> getUserZoneById(long userId)方法首先获取用户基本信息,调用
InstantService中创建的List<InstantDTO> getInstantDTOById(long user_id)方法获取用户动态,拼接到UserZoneDTO中
GET /user/{user_id}/zone
Request
从url中获取user_id
Response
{
"code": ,
"data": {
// UserZoneDTO 类
},
"msg":
}