一、统一返回数据格式
项目中我们会将响应封装成json返回,一般我们会将所有接口的数据格式统一, 使前端(iOS Android, Web)对数据的操作更一致、轻松。 一般情况下,统一返回数据格式没有固定的格式,只要能描述清楚返回的数据状态以及要返回的具体数据就可以。但是一般会包含状态码、返回消息、数据这几部分内容 例如,我们的系统要求返回的基本数据格式如下: 列表:{ "success": true, "code": 20000, "message": "成功", "data": { "items": [ { "id": "1", "name": "路飞", "intro": "海贼王路飞" } ] } }
分页:
{ "success": true, "code": 20000, "message": "成功", "data": { "total": 17, "rows": [ { "id": "1", "name": "鸣人", "intro": "火影七代" } ] } }
没有返回数据:
{ "success": true, "code": 20000, "message": "成功", "data": {} }
失败:
{ "success": false, "code": 20001, "message": "失败", "data": {} }
统一结果:
{ "success": 布尔, //响应是否成功 "code": 数字, //响应码 "message": 字符串, //返回消息 "data": HashMap //返回数据,放在键值对中 }
二、创建统一结果返回类
1.创建常量状态code
package com.stu.commonutils; public interface ResultCode { public static Integer SUCCESS = 20000; public static Integer ERROR = 20001; }
2.创建统一的返回结果类(基于lombok没有get和set方法)
package com.stu.commonutils; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import java.util.HashMap; import java.util.Map; @Data public class ResultData { @ApiModelProperty(value = "是否成功") private Boolean success; @ApiModelProperty(value = "返回码") private Integer code; @ApiModelProperty(value = "返回消息") private String message; @ApiModelProperty(value = "返回数据") private Map<String, Object> data = new HashMap<String, Object>(); private ResultData(){} public static ResultData success(){ ResultData r = new ResultData(); r.setSuccess(true); r.setCode(ResultCode.SUCCESS); r.setMessage("成功"); return r; } public static ResultData error(){ ResultData r = new ResultData(); r.setSuccess(false); r.setCode(ResultCode.ERROR); r.setMessage("失败"); return r; } public ResultData success(Boolean success){ this.setSuccess(success); return this; } public ResultData message(String message){ this.setMessage(message); return this; } public ResultData code(Integer code){ this.setCode(code); return this; } public ResultData data(String key, Object value){ this.data.put(key, value); return this; } public ResultData data(Map<String, Object> map){ this.setData(map); return this; } }
3.例子
package com.stu.eduservice.controller; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.stu.commonutils.ResultData; import com.stu.eduservice.entity.EduCourse; import com.stu.eduservice.entity.EduVideo; import com.stu.eduservice.entity.vo.CoursePublishVo; import com.stu.eduservice.entity.vo.CourseQuery; import com.stu.eduservice.entity.vo.CourseVo; import com.stu.eduservice.service.IEduCourseService; import org.apache.commons.lang3.StringUtils; 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 2021-05-29 */ @RestController @RequestMapping("/eduservice/course") @CrossOrigin public class EduCourseController { @Autowired private IEduCourseService eduCourseService; //条件查询,分页 @PostMapping("pageCourseCondition/{current}/{limit}") public ResultData pageTeacherCondition(@PathVariable long current, @PathVariable long limit, @RequestBody(required = false) CourseQuery courseQuery ){ QueryWrapper wrapper = new QueryWrapper(); String title = courseQuery.getTitle(); String status = courseQuery.getStatus(); if (!StringUtils.isEmpty(title)) { wrapper.like("title", title); } if (!StringUtils.isEmpty(status) ) { wrapper.eq("status", status); } Page<EduCourse> page = new Page<EduCourse>(current,limit); eduCourseService.page(page,wrapper); Map<String,Object> map = new HashMap<String,Object>(); long total = page.getTotal(); List<EduCourse> list = page.getRecords(); map.put("total",total); map.put("list",list); return ResultData.success().data(map);//链式编程 } }