1. 程式人生 > >java後端返回統一報文類

java後端返回統一報文類

package com.czqc.czc.buz.api.beans.vo;

import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;


public class ResponseStatus<T> implements Serializable {

    @ApiModelProperty("報文狀態: 0 失敗,1 成功,-1 未登入,-2 無許可權")
    private Integer status = 1;

    @ApiModelProperty("報文: 成功響應報文,失敗響應錯誤資訊")
    private T msg = (T) "ok";


    public static ResponseStatus ok() {
        return new ResponseStatus();
    }

    public static ResponseStatus list(Integer totalPages, Object content) {
        return new ResponseStatus(totalPages, content);
    }

    public static ResponseStatus object(Object content) {
        return new ResponseStatus(content);
    }

    public static ResponseStatus exception(Integer status, String errMsg) {
        return new ResponseStatus(status, errMsg);
    }


    public ResponseStatus(T content) {
        this.msg = content;
    }

    public ResponseStatus(Integer totalPages, T content) {
        ResponseMsgVO<T> responseMsgVO = new ResponseMsgVO<>();
        responseMsgVO.setTotalPages(totalPages);
        responseMsgVO.setContent(content);
        this.msg = (T) responseMsgVO;
    }

    public ResponseStatus(Integer status, String msg) {
        this.status = status;
        this.msg = (T) msg;
    }

    public ResponseStatus() {
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public T getMsg() {
        return msg;
    }

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

    @Override
    public String toString() {
        return "MsgStatus{" +
                "status=" + status +
                ", msg=" + msg +
                '}';
    }



    public static class ResponseMsgVO<T> implements  Serializable {

        @ApiModelProperty("總頁數")
        private Integer totalPages = 0;

        @ApiModelProperty("內容")
        private T content = (T) "ok";

        public ResponseMsgVO(Integer totalPages, T content) {
            this.totalPages = totalPages;
            this.content = content;
        }

        public ResponseMsgVO(T content) {
            this.content = content;
        }

        public ResponseMsgVO() {
        }

        public Integer getTotalPages() {
            return totalPages;
        }

        public void setTotalPages(Integer totalPages) {
            this.totalPages = totalPages;
        }

        public T getContent() {
            return content;
        }

        public void setContent(T content) {
            this.content = content;
        }

        @Override
        public String toString() {
            return "ResponseMsg0VO{" +
                    "totalPages=" + totalPages +
                    ", content=" + content +
                    '}';
        }
    }
}