接口调用ReponseResult的封装

2019-07-17  本文已影响0人  黄靠谱

封装ReponseResult的意义

如果仅仅返回一个对象的话,如果报错的话,前端并不知道具体的失败原因,对用户不友好。封装成BaseResponse的话,就可以解决这个问题。比如因为参数的格式不对,或者其它的业务原因导致接口调用失败

通用的Response,但是一般会扩展这个code 和 msg,封装成一个错误枚举值,来统一错误码 和 报错信息,也为前端统一处理一些异常逻辑

使用

作为调用方(一般是前端)(或者RPC的远程调用方)(既有义务去check responseCode ,又有义务去check data是否为空或者为null,因为当前如果没有满足条件的数据的话,返回一个null也是正常的)

作为服务方有义务在返回一个list的时候,至少不能是null,而是一个 [],避免报错

Demo

Base类

@Data
public class ClientBaseResponse<T> implements Serializable {
    private String code;
    private String msg;
    private T data;
    
    public static <T> BaseResponse<T> success(T data) {
        BaseResponse<T> resp = new BaseResponse();
        resp.setCode(ResponseCode.SUCCESS.getCode());
        resp.setMsg(ResponseCode.SUCCESS.getMsg());
        resp.setData(data);
        return resp;
    }


    public static <T> ClientBaseResponse<T> fail(ClientResponseCodeEnum clientResponseCodeEnum) {
        ClientBaseResponse<T> resp = new ClientBaseResponse();
        resp.setCode(clientResponseCodeEnum.getCode());
        resp.setMsg(clientResponseCodeEnum.getMsg());
        return resp;
    }

    public static <T> ClientBaseResponse<T> fail(ClientResponseCodeEnum clientResponseCodeEnum,String extraMsg) {
        ClientBaseResponse<T> resp = new ClientBaseResponse();
        resp.setCode(clientResponseCodeEnum.getCode());
        resp.setMsg(clientResponseCodeEnum.getMsg()+extraMsg);
        return resp;
    }
    
    public ClientBaseResponse() {
        this.code = ClientResponseCode.SUCCESS.getCode();
        this.msg = ClientResponseCode.SUCCESS.getMsg();
    }

    
}

ClientResponseCodeEnum 枚举值,一般会有getFormatMsg这个函数,在通用的msg里面可以添加一些额外的更detail的信息到前端

public enum ClientResponseCodeEnum {

    SUCCESS("000", "成功"),

    // 001 ~ 099 基本校验异常
    ILLEGAL_ARGUMENT("001", "参数错误:%s"),
    ILLEGAL_CONVERT("002", "参数转换错误:%s"),
    ILLEGAL_REQUEST("003", "未知请求"),
    INVALID_PARAMS_LENGTH("004", "参数长度错误:%s"),
    INVALID_PARAMETER_VALUE("005", "参数值非法:%s"),
    PARAMS_IS_EMPTY("006", "必传参数不能为空:%s"),
    REQUEST_TIMEOUT("007", "请求超时"),
    AUTH_ACCESS_DENIED("008", "拒绝访问"),
    MISS_REQUIRED_PARAMETER("009", "缺失必选参数:%s"),
    DATA_NOT_EXISTS("010", "数据不存在"),
    SYSTEM_IS_BUSY("011", "系统繁忙"),
    DUPLICATE_REQUEST("012", "重复的请求"),

    // 900 ~ 999 服务层面异常
    RATELIMITED("900", "服务过载,当前请求已被限流"),
    DEGRADATION("901", "服务已降级"),

    FAILURE("500", "请求服务发生内部错误,请联系相关同学解决!");

    private final String code;
    private final String msg;

    public String getCode() {
        return this.code;
    }

    public String getMsg() {
        return this.msg;
    }

    public String getFormatMsg(String... params){
        return String.format(this.getMsg(),params);
    }
}

使用

    if (list == null) {
        return ClientBaseResponse.fail(ClientResponseCodeEnum.ILLEGAL_ARGUMENT);
    }
    if(list.size()>2000){
        return ClientBaseResponse.fail(ClientResponseCodeEnum.FAILURE,"最多一次处理2000个Staff信息");
    }
    ***
    
    return ClientBaseResponse.success(Data);
上一篇 下一篇

猜你喜欢

热点阅读