统一数据处理返回类(即拿即用)

数据处理码信息枚举类

package com.example.demo.uitls.enums;

/**
 * @author wyl
 * @date 2021/12/6
 */
public enum ResultEnums {

    /**
     * 对比值
     */
    ONE(1, "1"),
    ZERO(0, "0"),
    /**
     * 统一返回结果
     */
    SUCCESS(200, "请求返回成功!"),
    RETWEETS(302, "请求转发成功!"),
    WRONG_REQ(4000, "错误请求!"),
    ERROR(5000, "代码运行逻辑错误(如null数据)或信息操作失败(如存储、修改等失败)!");

    private Integer code;
    private String info;

    ResultEnums(Integer code, String info) {
        this.code = code;
        this.info = info;
    }

    public static String getValue(Integer code) {
        for (ResultEnums ele : values()) {
            if (code.equals(ele.getCode())) {
                return ele.getInfo();
            }
        }
        return null;
    }

    public Integer getCode() {
        return code;
    }

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

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }
}

数据统一封装类

package com.example.demo.uitls;

import com.example.demo.uitls.enums.ResultEnums;

import java.io.Serializable;

import static com.example.demo.uitls.enums.ResultEnums.*;

/**
 * @author wyl
 * @date 2021/12/6
 */
public class ResponseUitls<T> implements Serializable {

    private Integer code;
    private boolean success;
    private T data;
    private String massage;

    public ResponseUitls(Integer code, boolean success, T data, String massage) {
        this.code = code;
        this.success = success;
        this.data = data;
        this.massage = massage;
    }

    public ResponseUitls(Integer code, T data, String massage) {
        if (ONE.getCode().equals(code)){
            this.code = SUCCESS.getCode();
            this.success = true;
        }else if (ZERO.getCode().equals(code)){
            this.code=ERROR.getCode();
            this.success = false;
        }else {
            this.code=code;
            this.success = !(data==null||data.equals(0));
        }
        this.data = data;
        this.massage=massage;
    }

    public ResponseUitls(Integer code, T data) {
        if (ONE.getCode().equals(code)){
            this.code = SUCCESS.getCode();
            this.massage= SUCCESS.getInfo();
            this.success = true;
        }else if (ZERO.getCode().equals(code)){
            this.code=ERROR.getCode();
            this.massage= ERROR.getInfo();
            this.success = false;
        }else {
            this.code=code;
            this.massage= ResultEnums.getValue(code);
            this.success = !(data==null||data.equals(ZERO.getCode()));
        }
        this.data = data;
    }

    public ResponseUitls(T data) {
        if (data==null||data.equals(ZERO.getCode())){
            this.code = ERROR.getCode();
            this.success = false;
            this.massage = ERROR.getInfo();
        }else {
            this.code = SUCCESS.getCode();
            this.success = true;
            this.massage = SUCCESS.getInfo();
        }
        this.data = data;
    }

    public Integer getCode() {
        return code;
    }

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

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public T getData() {
        return data;
    }

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

    public String getMassage() {
        return massage;
    }

    public void setMassage(String massage) {
        this.massage = massage;
    }

    @Override
    public String toString() {
        return "ResponseUitls{" +
                "code=" + code +
                ", status=" + success +
                ", data=" + data +
                ", massage='" + massage + '\'' +
                '}';
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值