美文网首页
Java通用返回结果类

Java通用返回结果类

作者: Codifier | 来源:发表于2019-10-22 15:31 被阅读0次
    1. Result类:用于后台返回给前台的结果
    public class Result {
    
        // 结果状态码
        private Integer code;
        // 结果消息
        private String msg;
        // 结果数据
        private Object data;
     
        public Result() {
            super();
        }
     
        public Result(Integer code, String msg, Object data) {
            this.code = code;
            this.msg = msg;
            this.data = data;
        }
        
        /**
         * 根据状态码枚举设置状态码和状态信息的值
         * @param code 状态码枚举
         */
        public void setResultCode(ResultCode code) {
            this.code = code.getCode();
            this.msg = code.getMessage();
        }
     
        public Integer getCode() {
            return code;
        }
     
        public void setCode(Integer code) {
            this.code = code;
        }
     
        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;
        }
     
        @Override
        public String toString() {
            return "Result{" +
                    "code=" + this.code +
                    ", msg='" + this.msg + '\'' +
                    ", data=" + this.data +
                  '}';
        }
    }
    
    1. ResultCode类:结果状态枚举
    public enum ResultCode {
    
        SUCCESS(10001, "成功"),
        FAILURE(10002, "失败"),
        UNKNOWN_ERROR(10003, "未知错误"),
        SYSTEM_INNER_ERROR(10004, "系统繁忙,请稍后重试"),
        PARAM_VALIDATE_SUCCESS(10005, "参数校验成功"),
    
        /* 参数错误:20001-29999 */
        PARAM_IS_INVALID(20001, "参数无效"),
        PARAM_IS_BLANK(20002, "参数为空"),
        PARAM_TYPE_BIND_ERROR(20003, "参数类型错误"),
        PARAM_NOT_COMPLETE(20004, "参数缺失");
    
        // 结果状态码
        private Integer code;
    
        // 结果消息
        private String message;
    
        ResultCode(Integer code, String message) {
            this.code = code;
            this.message = message;
        }
    
        public Integer getCode() {
            return code;
        }
    
        public String getMessage() {
            return message;
        }
    }
    
    1. ResultUtil类:返回结果工具类
    public class ResultUtil {
        
        /**
         * 成功无返回数据
         * @return
         */
        public static Result success() {
            Result result = new Result();
            result.setResultCode(ResultCode.SUCCESS);
            return result;
        }
        
        /**
         * 成功带返回数据
         * @param data 响应数据
         * @return
         */
        public static Result success(Object data) {
            Result result = new Result();
            result.setResultCode(ResultCode.SUCCESS);
            result.setData(data);
            return result;
        } 
        
        /**
         * 自定义状态码并带返回数据
         * @param code 结果状态码
         * @param data 响应数据
         * @return
         */
        public static Result success(ResultCode code, Object data) {
            Result result = new Result();
            result.setResultCode(code);
            result.setData(data);
            return result;
        }
        
        /**
         * 失败不带返回数据
         * @return
         */
        public static Result failure() {
            Result result = new Result();
            result.setResultCode(ResultCode.FAILURE);
            return result;
        }
        
        /**
         * 失败带返回数据
         * @param data 响应数据
         * @return
         */
        public static Result failure(Object data) {
            Result result = new Result();
            result.setResultCode(ResultCode.FAILURE);
            result.setData(data);
            return result;
        }
    }
    

    相关文章

      网友评论

          本文标题:Java通用返回结果类

          本文链接:https://www.haomeiwen.com/subject/fqbqvctx.html