springBoot公共返回库
自定义springBoot公共返回库
使用
<dependency>
<groupId>site.hanbin</groupId>
<artifactId>response-common</artifactId>
<version>0.0.3</version>
</dependency>
版本说明
version
2.6.n 为springboot 2.6.x 版本,使用 jdk1.8
2.7.n 为springboot 2.7.x 版本,使用 jdk1.8
3.0.n 为springboot 3.0.x 版本,使用 jdk17
3.3.n 为springboot 3.3.x 版本,使用 jdk17
page-2.6.n 为springboot 2.6.x 版本,使用 jdk1.8,允许使用分页
page-2.7.n 为springboot 2.7.x 版本,使用 jdk1.8,允许使用分页
page-3.0.n 为springboot 3.0.x 版本,使用 jdk17,允许使用分页
page-3.3.n 为springboot 3.3.x 版本,使用 jdk17,允许使用分页
x 与 n 非对应关系
代码使用
直接返回
return ResultUtils.success(1, "返回信息", Object 返回数据);
return ResultUtils.success();
return ResultUtils.successOnlyMsg("返回信息");
return ResultUtils.success(Object 返回数据);
return ResultUtils.error(1, "错误信息");
return ResultUtils.error("错误信息");
异常返回
throw new ApiException();
throw new ApiException("异常信息");
throw new ApiException(500, "异常信息");
分页返回
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import site.hanbin.response.common.page.model.PageConfig;
import com.hb.scan.dining.server.common.page.utils.PageUtils;
// 前端或者其他接收到的分页条数以及当前页数
Integer pageNum = 1;
Integer pageSize = 10;
PageConfig pageConfig = new PageConfig(pageNum, pageSize);
PageHelper.startPage(pageNum, pageSize);
// 获得list数据部分, mapper 可携带自定义参数
List<Object> adminListVos = dingTableMapper.selectDingTableList(name, status);
// 返回部分
PageUtils.getPageResult(pageConfig, new PageInfo<>(adminListVos));
自定义状态码
response:
success: 1 # 请求成功返回状态码
error: 0 # 请求失败返回状态码
notLogin: 400 # 用户未登录返回状态码
unknown: -1 # 未知异常返回状态码
tokenExpire: 401 # 用户登录token过期
noAccess: 402 # 无访问权限返回状态码
特点
- 适配不同版本的springBoot(2.6以及以下, 2.7, 3.0 以上)
- 适配分页部分
其他
- 适配不同版本的springBoot(2.6以及以下, 2.7, 3.0 以上)
- page-* 版本,必须配置 数据源,否则启动失败
仓库地址
仓库地址
本文是原创文章,采用 CC BY-NC-ND 4.0 协议,完整转载请注明来自 程序员小航
评论
匿名评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果