接口返回统一数据格式

2021年3月27日 12点热度 0条评论 来源: 预立科技

前端与后端服务分离时,后端服务常常需要定义统一的数据格式

以下面的json格式为例

{ 
  "code": 1,
  "msg": "成功",
  "data": [
    { 
      "id": null,
      "nickname": "小明",
      "sex": null
    },
    { 
      "id": null,
      "nickname": "小红",
      "sex": null
    }
  ]
}

code:请求状态
msg: 请求状态描述
data:具体业务数据,用泛型

一、首先创建请求状态枚举,用于返回数据中的code

public enum ResultCodeEnum { 
    /** * 否;禁用 */
    NO(0, "失败"),
    /** * 是;启用 */
    YES(1, "成功"),
    ;

    ResultCodeEnum(Integer status, String desc) { 
        this.status = status;
        this.desc = desc;
    }

    private Integer status;
    private String desc;

    public Integer getStatus() { 
        return status;
    }

    public String getDesc() { 
        return desc;
    }
}

二、统一数据返回类,使用泛型T,处理不同的具体业务

package com.pd.shop.common;

import com.pd.shop.enums.ResultCodeEnum;
import lombok.Data;

@Data
public class Result<T> { 
    private Integer code;

    private String msg;

    private T Data;

    public Result() { 

    }

    Result(ResultCodeEnum codeEnum) { 
        this.code = codeEnum.getStatus();
        this.msg = codeEnum.getDesc();
    }

    Result(ResultCodeEnum codeEnum, T t) { 
        this.code = codeEnum.getStatus();
        this.msg = codeEnum.getDesc();
        this.Data = t;
    }

    public Result success() { 
        return new Result(ResultCodeEnum.YES);
    }

    public Result fail() { 
        return new Result(ResultCodeEnum.NO);
    }

    public static <T> Result<T> success(T t) { 
        return new Result(ResultCodeEnum.YES, t);
    }

    public static <T> Result<T> fail(T t) { 
        return new Result(ResultCodeEnum.NO, t);
    }
}

三、创建基类Controller, Controller层继承BaseController, 方便Controller层封装响应结果

package com.pd.shop.controller;

import com.pd.shop.common.Result;

public class BaseController { 

    public Result success(){ 
        return new Result().success();
    }

    public Result fail(){ 
        return new Result().fail();
    }

    public <T> Result<T> success(T data){ 
        return Result.success(data);
    }

    public static <T> Result<T> fail(T data){ 
        return Result.fail(data);
    }
}

四、Controller层调用统一响应结果json

说明:@RestController中包含@ResponseBody, @ResponseBody会将对象转为json返回给前端页面

package com.pd.shop.controller;

import com.pd.shop.common.Result;
import com.pd.shop.model.entity.UserInfo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/v1/testApi")
public class TestApiController extends BaseController{ 

    @GetMapping("/success1")
    public Result<String> success1(){ 
        return success("dsdasaas");
    }

    @GetMapping("/success2")
    public Result success2(){ 
        return success();
    }

    @GetMapping("/success3")
    public Result<List<UserInfo>> success3(){ 
        List<UserInfo> list = new ArrayList<>();

        UserInfo userInfo = new UserInfo();
        userInfo.setNickname("小明");
        list.add(userInfo);

        UserInfo userInfo1 = new UserInfo();
        userInfo1.setNickname("小红");
        list.add(userInfo1);

        return success(list);
    }

    @GetMapping("/fail1")
    public Result fail1(){ 
        return fail();
    }

    @GetMapping("/fail2")
    public Result<List<UserInfo>> fail2(){ 

        List<UserInfo> list = new ArrayList<>();

        UserInfo userInfo = new UserInfo();
        userInfo.setNickname("小明");
        list.add(userInfo);

        UserInfo userInfo1 = new UserInfo();
        userInfo1.setNickname("小红");
        list.add(userInfo1);

        return fail(list);
    }
}
    原文作者:预立科技
    原文地址: https://blog.csdn.net/sinat_16998945/article/details/115259919
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。