線上教育專案(六)----統一返回資料格式

會開地鐵的碼農發表於2020-12-24

在這裡插入圖片描述

一、統一返回資料格式

專案中我們會將響應封裝成json返回,一般我們會將所有介面的資料格式統一, 使前端(iOS Android, Web)對資料的操作更一致、輕鬆。
一般情況下,統一返回資料格式沒有固定的格式,只要能描述清楚返回的資料狀態以及要返回的具體資料就可以。但是一般會包含狀態碼、返回訊息、資料這幾部分內容
例如,我們的系統要求返回的基本資料格式如下:
列表:

{
  "success": true,
  "code": 20000,
  "message": "成功",
  "data": {
    "items": [
      {
        "id": "1",
        "name": "張三",
        "intro": "近年主持國家自然科學基金(6項)、江蘇省重大科技成果轉化專案(5項)、江蘇省產學研前瞻性聯合研究專案(3項)、省工業科技支撐、省高技術、省自然科學基金等省部級及其企業的主要科研專案40多個,多個專案在企業成功轉化,產生了較好的經濟、社會和環境效益。積極開展產學研科技合作,並與省內16家企業建立了江蘇省研究生工作站,其中6家為江蘇省優秀研究生工作站",
        "career": "高階",
        "level": 1,
        "avatar": "https://guli-file-190513.oss-cn-beijing.aliyuncs.com/avatar/default.jpg",
        "sort": 0,
        "isDeleted": false,
        "gmtCreate": "2019-10-30 14:18:46",
        "gmtModified": "2019-11-12 13:36:36"
      },
      {
        "id": "1189389726308478977",
        "name": "晴天",
        "intro": "高階講師簡介",
        "career": "高階講師資歷",
        "level": 2,
        "avatar": "https://online-teach-file.oss-cn-beijing.aliyuncs.com/teacher/2019/10/30/de47ee9b-7fec-43c5-8173-13c5f7f689b2.png",
        "sort": 1,
        "isDeleted": false,
        "gmtCreate": "2019-10-30 11:53:03",
        "gmtModified": "2019-10-30 11:53:03"
      },
      {
        "id": "1189390295668469762",
        "name": "李剛",
        "intro": "高階講師簡介",
        "career": "高階講師",
        "level": 2,
        "avatar": "https://online-teach-file.oss-cn-beijing.aliyuncs.com/teacher/2019/10/30/b8aa36a2-db50-4eca-a6e3-cc6e608355e0.png",
        "sort": 2,
        "isDeleted": false,
        "gmtCreate": "2019-10-30 11:55:19",
        "gmtModified": "2019-11-12 13:37:52"
      },
      {
        "id": "1189426437876985857",
        "name": "王二",
        "intro": "高階講師簡介",
        "career": "高階講師",
        "level": 1,
        "avatar": "https://online-teach-file.oss-cn-beijing.aliyuncs.com/teacher/2019/11/08/e44a2e92-2421-4ea3-bb49-46f2ec96ef88.png",
        "sort": 0,
        "isDeleted": false,
        "gmtCreate": "2019-10-30 14:18:56",
        "gmtModified": "2019-11-12 13:37:35"
      },
      {
        "id": "1189426464967995393",
        "name": "王五",
        "intro": "高階講師簡介",
        "career": "高階講師",
        "level": 1,
        "avatar": "https://online-teach-file.oss-cn-beijing.aliyuncs.com/teacher/2019/10/30/65423f14-49a9-4092-baf5-6d0ef9686a85.png",
        "sort": 0,
        "isDeleted": false,
        "gmtCreate": "2019-10-30 14:19:02",
        "gmtModified": "2019-11-12 13:37:18"
      },
      {
        "id": "1192249914833055746",
        "name": "李四",
        "intro": "高階講師簡介",
        "career": "高階講師",
        "level": 1,
        "avatar": "https://online-teach-file.oss-cn-beijing.aliyuncs.com/teacher/2019/11/07/91871e25-fd83-4af6-845f-ea8d471d825d.png",
        "sort": 0,
        "isDeleted": false,
        "gmtCreate": "2019-11-07 09:18:25",
        "gmtModified": "2019-11-12 13:37:01"
      }
    ]
  }
}

沒有返回資料:

{
  "success": true,
  "code": 20000,
  "message": "成功",
  "data": {}
}

失敗:

{
  "success": false,
  "code": 20001,
  "message": "失敗",
  "data": {}
}

因此,我們定義統一結果

{
  "success": 布林, //響應是否成功
  "code": 數字, //響應碼
  "message": 字串, //返回訊息
  "data": HashMap //返回資料,放在鍵值對中
}

二、建立統一結果返回類

1、在common模組下建立子模組common-utils

在這裡插入圖片描述

2、建立介面定義返回碼

建立包com.atguigu.commonutils,建立介面 ResultCode.java

package com.atguigu.commonutils;

public interface ResultCode {
    public static Integer SUCCESS = 20000;//成功

    public static Integer ERROR = 20001;//失敗
}

3、建立結果類

建立類 R.java

package com.atguigu.commonutils;


import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

//統一返回結果的類
@Data
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<String, Object>();

    //把構造方法私有
    private R() {
    }
    //鏈式程式設計

    //成功靜態方法
    public static R ok() {
        R r = new R();
        r.setSuccess(true);
        r.setCode(ResultCode.SUCCESS);
        r.setMessage("成功");

        return r;
    }

    //失敗靜態方法
    public static R error() {
        R r = new R();
        r.setSuccess(false);
        r.setCode(ResultCode.ERROR);
        r.setMessage("失敗");

        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;

    }
}

二、統一返回結果使用

1、在service模組中新增依賴

<dependency>
    <groupId>com.atguigu</groupId>
    <artifactId>common_utils</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

2、修改Controller中的返回結果

列表

@Api(description = "講師管理")
@RestController
@RequestMapping("/edu/teacher")
public class TeacherController {

    //訪問地址:http://localhost:8001/edu/teacher/findAll

    //先把service注入進來

    @Autowired
    private TeacherService teacherService;

    //1、查詢表中所有資料
    //rest風格
    @ApiOperation(value = "所有講師列表")
    @GetMapping("findAll")
    public R findAll(){
        //呼叫Service中的方法做查詢操作
        List<Teacher> teachers=teacherService.list(null);
        return R.ok().data("items",teachers);
    }

刪除

@Api(description = "講師管理")
@RestController
@RequestMapping("/edu/teacher")
public class TeacherController {

    //訪問地址:http://localhost:8001/edu/teacher/findAll

    //先把service注入進來

    @Autowired
    private TeacherService teacherService;

    //2、邏輯刪除講師方法
    @ApiOperation(value = "邏輯刪除講師")
    @DeleteMapping("{id}")
    //id表示需要通過路徑傳遞值
    public  R removeTeacher(@ApiParam(name="id",value="講師ID",required = true)@PathVariable String id){
        boolean flag=teacherService.removeById(id);
        if(flag){
            return R.ok();
        }else {
            return R.error();
        }

相關文章