Result返回值包装

Posted chenzhengwei

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Result返回值包装相关的知识,希望对你有一定的参考价值。

package com.imooc.miaosha.result;

public class Result<T> {
    
    private int code;
    private String msg;
    private Object data;
    
    /**
     *  成功时候的调用
     * */
    public static  <T> Result<T> success(SuccessMsg successMsg){
        return new Result<T>(successMsg);
    }
    
    /**
     *  失败时候的调用
     * */
    public static  <T> Result<T> error(CodeMsg codeMsg){
        return new Result<T>(codeMsg);
    }
    
    private Result(T data) {
        this.data = data;
    }
    
    private Result(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }
    
    private Result(CodeMsg codeMsg) {
        if(codeMsg != null) {
            this.code = codeMsg.getCode();
            this.msg = codeMsg.getMsg();
            this.data = "发生异常,请查看异常信息";
        }
    }

    private Result(SuccessMsg successMsg) {
        if(successMsg != null) {
            this.code = successMsg.getCode();
            this.msg = successMsg.getMsg();
            this.data=successMsg.getObject();
        }
    }
    
    
    public int getCode() {
        return code;
    }
    public void setCode(int 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(T data) {
        this.data = data;
    }
}
package com.imooc.miaosha.result;

public class  CodeMsg {
    
    private int code;
    private String msg;
    
    //通用的错误码
    public static CodeMsg SUCCESS = new CodeMsg(200, "success");
    public static CodeMsg SERVER_ERROR = new CodeMsg(500100, "服务端异常");
    public static CodeMsg BIND_ERROR = new CodeMsg(500101, "参数校验异常:%s");
    //登录模块 5002XX
    public static CodeMsg SESSION_ERROR = new CodeMsg(500210, "Session不存在或者已经失效");
    public static CodeMsg PASSWORD_EMPTY = new CodeMsg(500211, "登录密码不能为空");
    public static CodeMsg MOBILE_EMPTY = new CodeMsg(500212, "手机号不能为空");
    public static CodeMsg MOBILE_ERROR = new CodeMsg(500213, "手机号格式错误");
    public static CodeMsg MOBILE_NOT_EXIST = new CodeMsg(500214, "手机号不存在");
    public static CodeMsg PASSWORD_ERROR = new CodeMsg(500215, "密码错误");
    
    //商品模块 5003XX

    
    //订单模块 5004XX
    
    //秒杀模块 5005XX
    public static CodeMsg MIAO_SHA_OVER = new CodeMsg(500500, "商品已经秒杀完毕");
    public static CodeMsg REPEATE_MIAOSHA = new CodeMsg(500501, "不能重复秒杀");



    //图片上传 5101XX
    public static CodeMsg IMAGE_MAX_ERROR = new CodeMsg(510101, "上传文件大小超过5MB限制");
    public static CodeMsg IMAGE_NOT_SELECT= new CodeMsg(510102, "请选择文件");
    public static CodeMsg IMAGE_NOT_FORMAT= new CodeMsg(510103, "上传文件扩展名是不允许的扩展名\n只允许gif,jpg,jpeg,png,bmp格式");

    private CodeMsg( ) {
    }
            
    private CodeMsg( int code,String msg ) {
        this.code = code;
        this.msg = msg;

    }
    
    public int getCode() {
        return code;
    }
    public void setCode(int code) {
        this.code = code;
    }
    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }
    
    public CodeMsg fillArgs(Object... args) {
        int code = this.code;
        String message = String.format(this.msg, args);
        return new CodeMsg(code, message);
    }

    @Override
    public String toString() {
        return "CodeMsg [code=" + code + ", msg=" + msg + "]";
    }
    
    
}
package com.imooc.miaosha.result;

/**
 * @author zhengwei.chen
 * @title: SuccessMsg
 * @projectName miaosha_idea5
 * @description: TODO
 * @date 2019/5/2810:33
 */
public class SuccessMsg {

    private int code;
    private String msg;
    private Object object;

    //返回值无参数返回的时候返回
    public static SuccessMsg SUCCESS = new SuccessMsg(200,"操作成功!","该方法无需返回值");
    //返回值中需要带参数返回
    public static SuccessMsg SUCCESS_WITH_RESULT(Object object){
        SuccessMsg successMsg = new SuccessMsg();
        successMsg.setCode(200);
        successMsg.setMsg("操作成功!");
        successMsg.setObject(object);
        return successMsg;
    }

    //构造方法
    public SuccessMsg() {}
    public SuccessMsg(int code, String msg,Object o) {
        this.code = code;
        this.msg = msg;
        this.object=o;
    }

    public int getCode() { return code; }
    public void setCode(int code) { this.code = code;}
    public String getMsg() {return msg; }
    public void setMsg(String msg) {this.msg = msg;}
    public Object getObject() { return object; }
    public void setObject(Object object) { this.object = object; }

    @Override
    public String toString() {
        return "{\"SuccessMsg\":{" + "\"code\":" + code + ",\"msg\":\"" + msg + ‘\"‘+ ",\"object\":"+ object+ "}}";}
}

 

以上是关于Result返回值包装的主要内容,如果未能解决你的问题,请参考以下文章

忽略使用“warn_unused_result”属性声明的函数的返回值

忽略使用“warn_unused_result”属性声明的函数的返回值

php 一个自定义的try..catch包装器代码片段,用于执行模型函数,使其成为一个单行函数调用

Android:片段在活动结果合同中未收到 RESULT_OK

Finally-操作返回值

如何组合绑定片段而不将它们包装在 XML 文字中