返回对象定义

该博客主要介绍了一个泛型响应类`Response`的设计与实现,包括成功和失败状态的设置方法,以及如何从`ResponseService`获取响应码和消息。类中包含了标志(flag)、状态码(code)、消息(msg)和数据(data)字段,提供了便捷的构建成功和失败响应的方法,并实现了`Serializable`接口。此外,还讨论了如何检查响应的成功与失败,并提供了转化为字符串的实现。
摘要由CSDN通过智能技术生成
public class Response<T> implements Serializable {
    private static final long serialVersionUID = 3715837692826438329L;
    private String flag;
    private String code;
    private String msg;
    private T data;

    public Response() {
    }

    public Response success() {
        this.flag = RespFlagEnum.SUCCESS.getCode();
        return this;
    }

    public Response success(T data) {
        this.data = data;
        return this.success();
    }

    /** @deprecated */
    @Deprecated
    public Response success(String code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        return this.success(data);
    }

    public Response success(ResponseService responseService, T data) {
        this.code = responseService.getResponseCode();
        this.msg = responseService.getResponseMessage();
        return this.success(data);
    }

    public Response success(ResponseService responseService) {
        if (responseService == null) {
            return this.success();
        } else {
            this.code = responseService.getResponseCode();
            this.msg = responseService.getResponseMessage();
            return this.success();
        }
    }

    public boolean checkIfSuccess() {
        return RespFlagEnum.SUCCESS.getCode().equals(this.flag);
    }

    public boolean checkIfFail() {
        return !this.checkIfSuccess();
    }

    public Response fail() {
        this.flag = RespFlagEnum.FAIL.getCode();
        return this;
    }

    /** @deprecated */
    @Deprecated
    public Response fail(String code, String msg) {
        this.code = code;
        this.msg = msg;
        return this.fail();
    }

    public Response fail(ResponseService responseService) {
        this.code = responseService.getResponseCode();
        this.msg = responseService.getResponseMessage();
        return this.fail();
    }

    public Response fail(ResponseService responseService, T data) {
        this.code = responseService.getResponseCode();
        this.msg = responseService.getResponseMessage();
        this.data = data;
        return this.fail();
    }

    public Response fail(Response response) {
        this.code = response.getCode();
        this.msg = response.getMsg();
        return this.fail();
    }

    public ResponseService genResponseService() {
        return new DefaultCodeMsgResponse(this.code, this.msg);
    }

    public String getFlag() {
        return this.flag;
    }

    public void setFlag(String flag) {
        this.flag = flag;
    }

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

    public void setCode(String code) {
        this.code = code;
    }

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

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return this.data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String toString() {
        return JSON.toJSONString(this);
    }
}

成功:Date有数据,flag为S

失败:Date没有数据,flage为F,且自动将错误枚举的errorCode和errorMsg走动复制给code和msg(枚举继承ResponseService)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值