springboot 统一返回数据格式
一、统一返回数据格式
项目中我们会将响应封装成json返回,一般我们会将所有接口的数据格式统一, 使前端(iOS Android, Web)对数据的操作更一致、轻松。
一般情况下,统一返回数据格式没有固定的格式,只要能描述清楚返回的数据状态以及要返回的具体数据就可以。但是一般会包含状态码、返回消息、数据这几部分内容
例如,我们的系统要求返回的基本数据格式如下:
列表:
{ "success": true, "code": 20000, "message": "成功", "data": { "items": [ { "id": "1", "name": "刘德华", "intro": "毕业于师范大学数学系,热爱教育事业,执教数学思维6年有余" } ] } }
分页:
{ "success": true, "code": 20000, "message": "成功", "data": { "total": 17, "rows": [ { "id": "1", "name": "刘德华", "intro": "毕业于师范大学数学系,热爱教育事业,执教数学思维6年有余" } ] } }
没有返回数据:
{ "success": true, "code": 20000, "message": "成功", "data": {} }
失败:
{ "success": false, "code": 20001, "message": "失败", "data": {} }
因此,我们定义统一结果
{ "success": 布尔, //响应是否成功 "code": 数字, //响应码 "message": 字符串, //返回消息 "data": HashMap //返回数据,放在键值对中 }
package com.stu.service.base.result; import lombok.Getter; import lombok.ToString; /** * 前后端数据交换状态码 */ @Getter @ToString public enum ResultCodeEnum { SUCCESS(true, 20000, "成功"), UNKNOWN_REASON(false, 20001, "未知错误"), UPDATE_ERROR(false, 20002, "更新失败"), BAD_SQL_GRAMMAR(false, 21001, "sql 语法错误"), JSON_PARSE_ERROR(false, 21002, "json 解析异常"), PARAM_ERROR(false, 21003, "参数不正确"), FILE_UPLOAD_ERROR(false, 21004, "文件上传错误"), FILE_DELETE_ERROR(false, 21005, "文件刪除错误"), EXCEL_DATA_IMPORT_ERROR(false, 21006, "Excel 数据导入错误"), VIDEO_UPLOAD_ALIYUN_ERROR(false, 22001, "视频上传至阿里云失败"), VIDEO_UPLOAD_TOMCAT_ERROR(false, 22002, "视频上传至业务服务器失败"), VIDEO_DELETE_ALIYUN_ERROR(false, 22003, "阿里云视频文件删除失败"), FETCH_VIDEO_UPLOADAUTH_ERROR(false, 22004, "获取上传地址和凭证失败"), REFRESH_VIDEO_UPLOADAUTH_ERROR(false, 22005, "刷新上传地址和凭证失败"), FETCH_PLAYAUTH_ERROR(false, 22006, "获取播放凭证失败"), URL_ENCODE_ERROR(false, 23001, "URL编码失败"), ILLEGAL_CALLBACK_REQUEST_ERROR(false, 23002, "非法回调请求"), FETCH_ACCESSTOKEN_FAILD(false, 23003, "获取 accessToken 失败"), FETCH_USERINFO_ERROR(false, 23004, "获取用户信息失败"), LOGIN_ERROR(false, 23005, "登录失败"), COMMENT_EMPTY(false, 24006, "评论内容必须填写"), PAY_RUN(false, 25000, "支付中"), PAY_UNIFIEDORDER_ERROR(false, 25001, "统一下单错误"), PAY_ORDERQUERY_ERROR(false, 25002, "查询支付结果错误"), ORDER_EXIST_ERROR(false, 25003, "课程已购买"), GATEWAY_ERROR(false, 26000, "服务不能访问"), CODE_ERROR(false, 28000, "验证码错误"), LOGIN_PHONE_ERROR(false, 28009, "手机号码不正确"), LOGIN_MOBILE_ERROR(false, 28001, "账号不正确"), LOGIN_PASSWORD_ERROR(false, 28008, "密码不正确"), LOGIN_DISABLED_ERROR(false, 28002, "该用户已被禁用"), REGISTER_MOBLE_ERROR(false, 28003, "手机号已被注册"), LOGIN_AUTH(false, 28004, "需要登录"), LOGIN_ACL(false, 28005, "没有权限"), SMS_SEND_ERROR(false, 28006, "短信发送失败"), SMS_SEND_ERROR_BUSINESS_LIMIT_CONTROL(false, 28007, "短信发送过于频繁"), DIVIDE_ZERO(false, 29001, "除零错误"), DATA_NULL(false, 30001, "数据不存在!"), DATA_EXITS(false, 30002, "数据已存在!"), DATA_NODE_EXITS(false, 30003, "该章节下存在视频课程,请先删除视频课程!"); private final Boolean success; private final Integer code; private final String message; ResultCodeEnum(Boolean success, Integer code, String message) { this.success = success; this.code = code; this.message = message; } }
package com.stu.service.base.result; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import java.util.HashMap; import java.util.Map; /** - 全局统一返回结果 */ @Data @ApiModel(value = "全局统一返回结果") public class R { @ApiModelProperty(value = "是否成功") private Boolean success; @ApiModelProperty(value = "返回码") private Integer code; @ApiModelProperty(value = "返回消息") private String message; @ApiModelProperty(value = "返回数据") private Map<String, Object> data = new HashMap<>(); public R() { } public static R ok() { R r = new R(); r.setSuccess(ResultCodeEnum.SUCCESS.getSuccess()); r.setCode(ResultCodeEnum.SUCCESS.getCode()); r.setMessage(ResultCodeEnum.SUCCESS.getMessage()); return r; } public static R error() { R r = new R(); r.setSuccess(ResultCodeEnum.UNKNOWN_REASON.getSuccess()); r.setCode(ResultCodeEnum.UNKNOWN_REASON.getCode()); r.setMessage(ResultCodeEnum.UNKNOWN_REASON.getMessage()); return r; } public static R setResult(ResultCodeEnum resultCodeEnum) { R r = new R(); r.setSuccess(resultCodeEnum.getSuccess()); r.setCode(resultCodeEnum.getCode()); r.setMessage(resultCodeEnum.getMessage()); return r; } public R success(Boolean success) { this.setSuccess(success); return this; } public R message(String message) { this.setMessage(message); return this; } public R code(Integer code) { this.setCode(code); return this; } public R data(String key, Object value) { this.data.put(key, value); return this; } public R data(Map<String, Object> map) { this.setData(map); return this; } }
案例
package com.stu.service.edu.controller.admin; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.stu.service.base.result.R; import com.stu.service.edu.entity.Teacher; import com.stu.service.edu.entity.vo.ChapterVo; import com.stu.service.edu.entity.vo.TeacherQueryVo; import com.stu.service.edu.service.TeacherService; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.HashMap; import java.util.List; import java.util.Map; /** * <p> * 讲师 前端控制器 * </p> * * @author stu * @since 2022-04-11 */ @RestController @RequestMapping("/admin/edu/teacher") //@CrossOrigin public class TeacherController { @Autowired private TeacherService teacherService; /*********************************** * 用途说明:批量删除 * 返回值说明: com.stu.service.base.result.R ***********************************/ @DeleteMapping("batchRemoveTeacher") public R batchRemoveTeacher(@RequestBody List<String> idList) { boolean result = teacherService.removeByIds(idList); if (result) { return R.ok().message("删除成功"); } return R.error().message("删除失败"); } /*********************************** * 用途说明:删除讲师 * 返回值说明: com.stu.service.base.result.R ***********************************/ @DeleteMapping("deleteTeacher/{id}") public R deleteTeacher(@PathVariable String id) { /* if (true) { throw new CustomException(ResultCodeEnum.DIVIDE_ZERO); }*/ boolean result = teacherService.removeById(id); if (result) { return R.ok().message("删除成功"); } return R.error().message("删除失败"); } /*********************************** * 用途说明:修改讲师 * 返回值说明: com.stu.service.base.result.R ***********************************/ @PostMapping("updateTeacher") public R updateTeacher(@RequestBody Teacher teacher) { boolean result = teacherService.updateById(teacher); if (result) { return R.ok().message("更新成功"); } return R.error().message("更新失败"); } /*********************************** * 用途说明:添加讲师 * 返回值说明: com.stu.service.base.result.R ***********************************/ @ApiOperation("添加讲师") @PostMapping("addTeacher") public R addTeacher(@ApiParam("讲师对象") @RequestBody Teacher teacher) { boolean result = teacherService.save(teacher); if (result) { return R.ok().message("保存成功"); } return R.error().message("保存失败"); } /*********************************** * 用途说明:查询讲师表所有数据 * 返回值说明: com.stu.service.base.result.R ***********************************/ @GetMapping("findAll") public R findAllTeacher() { List<Teacher> list = teacherService.list(); return R.ok().data("list", list); } /*********************************** * 用途说明:查询 * 返回值说明: com.stu.service.edu.entity.Teacher ***********************************/ @GetMapping("get/{id}") public R getTeacher(@PathVariable String id) { return R.ok().data("dataInfo", teacherService.getById(id)); } /*********************************** * 用途说明:查询讲师表所有数据 * 返回值说明: com.stu.service.base.result.R ***********************************/ @GetMapping("pageList/{page}/{limit}") public R pageList(@PathVariable long page, @PathVariable long limit, TeacherQueryVo teacherQueryVo) { Page<Teacher> pageParam = new Page<>(page, limit); teacherService.listPage(pageParam, teacherQueryVo); Map<String, Object> map = new HashMap<String, Object>(); long total = pageParam.getTotal(); List<Teacher> list = pageParam.getRecords(); map.put("total", total); map.put("list", list); return R.ok().data(map); } }
作者:明
出处:https://www.cnblogs.com/konglxblog//
版权:本文版权归作者和博客园共有
转载:欢迎转载,文章中请给出原文连接,此文章仅为个人知识学习分享,否则必究法律责任
出处:https://www.cnblogs.com/konglxblog//
版权:本文版权归作者和博客园共有
转载:欢迎转载,文章中请给出原文连接,此文章仅为个人知识学习分享,否则必究法律责任