码迷,mamicode.com
首页 > 其他好文 > 详细

woshi

时间:2018-05-03 12:06:18      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:void   set   serve   return   success   .data   ret   boolean   inter   

package top.symind.base;

/**
* API 格式封装
* @author coder
*
*/
public class ApiResponse {
    private int code;
    private String message;
    private Object data;
    private boolean more;
    
    public ApiResponse(int code, String message, Object data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }
    
    public ApiResponse() {
        this.code = Status.SUCCESS.getCode();
        this.message = Status.SUCCESS.getStandardMessage();
    }
    
    public int getCode() {
        return code;
    }
    public void setCode(int code) {
        this.code = code;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
    public Object getData() {
        return data;
    }
    public void setData(Object data) {
        this.data = data;
    }
    public boolean isMore() {
        return more;
    }
    public void setMore(boolean more) {
        this.more = more;
    }
    
    public static ApiResponse ofMessage(int code, String message) {
        return new ApiResponse(code, message, null);
    }
    
    public static ApiResponse ofSuccess(Object data) {
        return new ApiResponse(Status.SUCCESS.getCode(), Status.SUCCESS.getStandardMessage(), data);
    }
    
    public static ApiResponse ofStatus(Status status) {
        return new ApiResponse(status.getCode(), status.getStandardMessage(), null);
    }
    
    public enum Status {
        SUCCESS(200, "OK"),
        BAD_REQUEST(400, "Bad Request"),
        NOT_FOUND(404, ""),
        INTERNAL_SERVER_ERROR(500, "Unknown Internal Error"),
        NOT_VALID_PARAM(40005, "Not Valid Params"),
        NOT_SUPPORTED_OPERATION(40006, "Operration Not Support"),
        NOT_LOGIN(50000, "Not Login");
        
        private int code;
        private String standardMessage;
        
        Status(int code, String standardMessage) {
            this.code = code;
            this.standardMessage = standardMessage;
        }

        public int getCode() {
            return code;
        }

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

        public String getStandardMessage() {
            return standardMessage;
        }

        public void setStandardMessage(String standardMessage) {
            this.standardMessage = standardMessage;
        }
        
    }
}

woshi

标签:void   set   serve   return   success   .data   ret   boolean   inter   

原文地址:https://www.cnblogs.com/SyMind/p/8984642.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!