封装返回结果类和枚举错误码

1.枚举错误码

/**
 * 枚举错误码
 */
public emum ErrorCode{

    FAILED(601,"操作失败"),
    AUTH_ERROR(401,"认证失败"),
    SYS_ERROR(500,"系统错误"),
    PARAM_ERROR(400,"参数错误"),
    UNKNOWN_ERROR(499,"未知错误");

    private int code;
    private String message;
    
    private ErrorCode(int code, String message) {
    	this.code=code;
        this.message=message;
    }
    
    public String getMessage() {
        return this.message;
    }
    public int getCode() {
        return this.code;
    }
}

2.封装返回结果类

/**
 * 返回结果类
 */
public class JsonResult {
 
	private int status = 0;
 
	private String msg = "success";
 
	private Object data;
 
        // 几个常用方法
	public JsonResult success() {
            return new JsonResult();
        }

        public JsonResult success(Object data) {
            return new JsonResult(data);
        }

        public JsonResult error(ErrorCode errorCode) {
            return new JsonResult(errorCode.getCode(), errorCode.getMessage());
        }

        public JsonResult error(int state, String message) {
            return new JsonResult(state, message);
        }

        public JsonResult error(int state, String message, Object data) {
            return new JsonResult(state, message, data);
        }

        // 几个构造方法
        public JsonResult() {
        }

        public JsonResult(Object data) {
            this.data = data;
        }

        public JsonResult(Integer state, String message) {
            this.state = state;
            this.message = message;
        }

        public JsonResult(Integer state, String message, Object data) {
            this.state = state;
            this.message = message;
            this.data = data;
        }
 
	public Integer getStatus() {
		return status;
	}
 
	public void setStatus(Integer status) {
		this.status = status;
	}
 
	public String getMsg() {
		return msg;
	}
 
	public void setMsg(String msg) {
		this.msg = msg;
	}
 
	public Object getData() {
		return data;
	}
 
	public void setData(Object data) {
		this.data = data;
	}
}

完美!!!

你可能感兴趣的:(封装返回结果类和枚举错误码)