MENU

SpringBoot返回统一格式的ResponseData<T>

Entity

import com.wanvale.enums.ResultEnums;

import java.io.Serializable;

public class RespData<T> implements Serializable {

    private String code;

    private String msg;

    private T data;


    public RespData(String code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public RespData(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public RespData(ResultEnums resultEnums) {
        this.code = resultEnums.getCode();
        this.msg = resultEnums.getMsg();
    }

    public RespData(ResultEnums resultEnums, T data) {
        this.code = resultEnums.getCode();
        this.msg = resultEnums.getMsg();
        this.data = data;
    }

    public RespData() {
    }

    public String getCode() {
        return code;
    }

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

    public String getMsg() {
        return msg;
    }

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

    public T getData() {
        return data;
    }

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

Enums


public enum ResultEnums {

    SUCCESS("0000", "请求成功"),
    ERROR("1111", "请求失败"),
    SYSTEM_ERROR("1000", "系统异常"),
    BUSSINESS_ERROR("2001", "业务逻辑错误"),
    VERIFY_CODE_ERROR("2002", "业务参数错误"),
    PARAM_ERROR("2002", "业务参数错误");

    private String code;
    private String msg;

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

    public String getCode() {
        return code;
    }

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

    public String getMsg() {
        return msg;
    }

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

Util

import com.wanvale.entity.RespData;
import com.wanvale.enums.ResultEnums;

public class RespDataUtil {
    /**
     * 带实体的统一返回
     *
     * @param data 实体
     * @param <T>  实体类型
     * @return
     */
    public static <T> RespData buildSuccess(T data) {
        return new RespData<T>(ResultEnums.SUCCESS, data);
    }

    public static RespData buildSuccess() {
        return new RespData(ResultEnums.SUCCESS);
    }

    public static RespData buildSuccess(String msg) {
        return new RespData(ResultEnums.SUCCESS.getCode(), msg);
    }

    public static RespData buildSuccess(String code, String msg) {
        return new RespData(code, msg);
    }

    public static <T> RespData buildSuccess(String code, String msg, T data) {
        return new RespData<T>(code, msg, data);
    }

    public static RespData buildSuccess(ResultEnums resultEnums) {
        return new RespData(resultEnums);
    }

    public static <T> RespData buildError(T data) {
        return new RespData<T>(ResultEnums.ERROR, data);
    }

    public static RespData buildError() {
        return new RespData(ResultEnums.ERROR);
    }

    public static RespData buildError(String msg) {
        return new RespData(ResultEnums.ERROR.getCode(), msg);
    }

    public static RespData buildError(String code, String msg) {
        return new RespData(code, msg);
    }

    public static <T> RespData buildError(String code, String msg, T data) {
        return new RespData<T>(code, msg, data);
    }

    public static RespData buildError(ResultEnums resultEnums) {
        return new RespData(resultEnums);
    }

文章标题:SpringBoot返回统一格式的ResponseData<T>
如果文中内容侵犯了您的权益,请及时与博主取得联系进行删除!
本站文章未经许可禁止转载,本文地址:https://blog.wanvale.com/archives/129/

Last Modified: January 9, 2021
Archives QR Code Tip
QR Code for this page
Tipping QR Code