Java 返回值规范(封装返回对象)

所需枚举类

public enum CodeEnum {
    SUCCESS(0),
    ERROR(1);

    private Integer code;

    private CodeEnum(Integer code) {
        this.code = code;
    }

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

简单对象返回:

返回值示例:

{
    "datas": ,
    "resp_code": 0,
    "resp_msg": ""
}

类:

import java.io.Serializable;

public class Result<T> implements Serializable {
    private T datas;
    private Integer resp_code;
    private String resp_msg;

    public static <T> Result<T> succeed(String msg) {
        return of((Object)null, CodeEnum.SUCCESS.getCode(), msg);
    }

    public static <T> Result<T> succeed(T model, String msg) {
        return of(model, CodeEnum.SUCCESS.getCode(), msg);
    }

    public static <T> Result<T> succeed(T model) {
        return of(model, CodeEnum.SUCCESS.getCode(), "SUCCESS");
    }

    public static <T> Result<T> of(T datas, Integer code, String msg) {
        return new Result(datas, code, msg);
    }

    public static <T> Result<T> failed(String msg) {
        return of((Object)null, CodeEnum.ERROR.getCode(), msg);
    }

    public static <T> Result<T> failed(T model, String msg) {
        return of(model, CodeEnum.ERROR.getCode(), msg);
    }

    public static <T> Result<T> failed(BusinessExceptionEnum businessExceptionEnum) {
        return of((Object)null, businessExceptionEnum.getCode(), businessExceptionEnum.getMessage());
    }

    public static <T> Result<T> failed(BusinessExceptionEnum businessExceptionEnum, String message) {
        return of((Object)null, businessExceptionEnum.getCode(), message);
    }

    public T getDatas() {
        return this.datas;
    }

    public Integer getResp_code() {
        return this.resp_code;
    }

    public String getResp_msg() {
        return this.resp_msg;
    }

    public void setDatas(T datas) {
        this.datas = datas;
    }

    public void setResp_code(Integer resp_code) {
        this.resp_code = resp_code;
    }

    public void setResp_msg(String resp_msg) {
        this.resp_msg = resp_msg;
    }

    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof Result)) {
            return false;
        } else {
            Result<?> other = (Result)o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                label47: {
                    Object this$resp_code = this.getResp_code();
                    Object other$resp_code = other.getResp_code();
                    if (this$resp_code == null) {
                        if (other$resp_code == null) {
                            break label47;
                        }
                    } else if (this$resp_code.equals(other$resp_code)) {
                        break label47;
                    }

                    return false;
                }

                Object this$datas = this.getDatas();
                Object other$datas = other.getDatas();
                if (this$datas == null) {
                    if (other$datas != null) {
                        return false;
                    }
                } else if (!this$datas.equals(other$datas)) {
                    return false;
                }

                Object this$resp_msg = this.getResp_msg();
                Object other$resp_msg = other.getResp_msg();
                if (this$resp_msg == null) {
                    if (other$resp_msg != null) {
                        return false;
                    }
                } else if (!this$resp_msg.equals(other$resp_msg)) {
                    return false;
                }

                return true;
            }
        }
    }

    protected boolean canEqual(Object other) {
        return other instanceof Result;
    }

    public int hashCode() {
        int PRIME = true;
        int result = 1;
        Object $resp_code = this.getResp_code();
        int result = result * 59 + ($resp_code == null ? 43 : $resp_code.hashCode());
        Object $datas = this.getDatas();
        result = result * 59 + ($datas == null ? 43 : $datas.hashCode());
        Object $resp_msg = this.getResp_msg();
        result = result * 59 + ($resp_msg == null ? 43 : $resp_msg.hashCode());
        return result;
    }

    public String toString() {
        return "Result(datas=" + this.getDatas() + ", resp_code=" + this.getResp_code() + ", resp_msg=" + this.getResp_msg() + ")";
    }

    public Result() {
    }

    public Result(T datas, Integer resp_code, String resp_msg) {
        this.datas = datas;
        this.resp_code = resp_code;
        this.resp_msg = resp_msg;
    }
}

 复杂对象返回(如分页对象)

返回值示例:

{
	"datas": [
		{}
	],
	"resp_code": 0,
	"resp_msg": "",
	"total": 0
}

类:

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.io.Serializable;
import java.util.List;

public class PageResult<T> implements Serializable {
    private static final long serialVersionUID = -275582248840137389L;
    private Integer total;
    private Integer resp_code;
    private String resp_msg;
    private List<T> datas;

    public static <T> PageResult<T> result(long total, List<T> list) {
        return new PageResult(Math.toIntExact(total), 200, "success", list);
    }

    public static <T> PageResult<T> result(Integer total, List<T> list) {
        return new PageResult(total, 200, "success", list);
    }

    public static <T> PageResult<T> result(Integer total, List<T> list, String msg) {
        return new PageResult(total, 200, msg, list);
    }

    public static <T> PageResult<T> result(Page<T> page) {
        return new PageResult(Convert.toInt(page.getTotal()), 200, "success", page.getRecords());
    }

    public static <T> PageResult.PageResultBuilder<T> builder() {
        return new PageResult.PageResultBuilder();
    }

    public Integer getTotal() {
        return this.total;
    }

    public Integer getResp_code() {
        return this.resp_code;
    }

    public String getResp_msg() {
        return this.resp_msg;
    }

    public List<T> getDatas() {
        return this.datas;
    }

    public void setTotal(Integer total) {
        this.total = total;
    }

    public void setResp_code(Integer resp_code) {
        this.resp_code = resp_code;
    }

    public void setResp_msg(String resp_msg) {
        this.resp_msg = resp_msg;
    }

    public void setDatas(List<T> datas) {
        this.datas = datas;
    }

    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof PageResult)) {
            return false;
        } else {
            PageResult<?> other = (PageResult)o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                label59: {
                    Object this$total = this.getTotal();
                    Object other$total = other.getTotal();
                    if (this$total == null) {
                        if (other$total == null) {
                            break label59;
                        }
                    } else if (this$total.equals(other$total)) {
                        break label59;
                    }

                    return false;
                }

                Object this$resp_code = this.getResp_code();
                Object other$resp_code = other.getResp_code();
                if (this$resp_code == null) {
                    if (other$resp_code != null) {
                        return false;
                    }
                } else if (!this$resp_code.equals(other$resp_code)) {
                    return false;
                }

                Object this$resp_msg = this.getResp_msg();
                Object other$resp_msg = other.getResp_msg();
                if (this$resp_msg == null) {
                    if (other$resp_msg != null) {
                        return false;
                    }
                } else if (!this$resp_msg.equals(other$resp_msg)) {
                    return false;
                }

                Object this$datas = this.getDatas();
                Object other$datas = other.getDatas();
                if (this$datas == null) {
                    if (other$datas != null) {
                        return false;
                    }
                } else if (!this$datas.equals(other$datas)) {
                    return false;
                }

                return true;
            }
        }
    }

    protected boolean canEqual(Object other) {
        return other instanceof PageResult;
    }

    public int hashCode() {
        int PRIME = true;
        int result = 1;
        Object $total = this.getTotal();
        int result = result * 59 + ($total == null ? 43 : $total.hashCode());
        Object $resp_code = this.getResp_code();
        result = result * 59 + ($resp_code == null ? 43 : $resp_code.hashCode());
        Object $resp_msg = this.getResp_msg();
        result = result * 59 + ($resp_msg == null ? 43 : $resp_msg.hashCode());
        Object $datas = this.getDatas();
        result = result * 59 + ($datas == null ? 43 : $datas.hashCode());
        return result;
    }

    public String toString() {
        return "PageResult(total=" + this.getTotal() + ", resp_code=" + this.getResp_code() + ", resp_msg=" + this.getResp_msg() + ", datas=" + this.getDatas() + ")";
    }

    public PageResult() {
    }

    public PageResult(Integer total, Integer resp_code, String resp_msg, List<T> datas) {
        this.total = total;
        this.resp_code = resp_code;
        this.resp_msg = resp_msg;
        this.datas = datas;
    }

    public static class PageResultBuilder<T> {
        private Integer total;
        private Integer resp_code;
        private String resp_msg;
        private List<T> datas;

        PageResultBuilder() {
        }

        public PageResult.PageResultBuilder<T> total(Integer total) {
            this.total = total;
            return this;
        }

        public PageResult.PageResultBuilder<T> resp_code(Integer resp_code) {
            this.resp_code = resp_code;
            return this;
        }

        public PageResult.PageResultBuilder<T> resp_msg(String resp_msg) {
            this.resp_msg = resp_msg;
            return this;
        }

        public PageResult.PageResultBuilder<T> datas(List<T> datas) {
            this.datas = datas;
            return this;
        }

        public PageResult<T> build() {
            return new PageResult(this.total, this.resp_code, this.resp_msg, this.datas);
        }

        public String toString() {
            return "PageResult.PageResultBuilder(total=" + this.total + ", resp_code=" + this.resp_code + ", resp_msg=" + this.resp_msg + ", datas=" + this.datas + ")";
        }
    }
}

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mikasa_akm

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值