ツールクラスのControllerレイヤは結果パッケージに戻ります

2955 ワード

一、列挙類ResultCodeEnum


すべての戻り符号化と意味、その後は自由に拡張できます
/**
 * @program: dc2
 * @description:  
 * @author: Mario
 * @create: 2019-07-22 16:21
 **/
public enum ResultCodeEnum {
    /**
     *  
     */
    SUCCESS(0, " "),

    /**
     *  
     */
    FAIL(-1, " ");


    /**
     *  
     */
    private Integer code;

    /**
     *  
     */
    private String msg;

    ResultCodeEnum(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    /**
     *  
     */
    public Integer code() {
        return this.code;
    }

    /**
     *  
     */
    public String msg() {
        return this.msg;
    }

    /**
     *  -- 
     */
    public static Integer getCode(String name) {
        for (ResultCodeEnum resultCode : ResultCodeEnum.values()) {
            if (resultCode.name().equals(name)) {
                return resultCode.code;
            }
        }
        return null;
    }

/*    public static void main(String[] args) {
        System.out.println(ResultCodeEnum.FAIL.code());
        System.out.println(ResultCodeEnum.SUCCESS.msg());
        System.out.println(ResultCodeEnum.getCode("SUCCESS"));
    }*/
}

二、結果カプセル化クラスに戻る

/**
 * @program: dc2
 * @description:  
 * @author: Mario
 * @create: 2019-07-22 16:21
 **/
@Getter  // set , set 
public class ResultUtil implements Serializable {
    private static final long serialVersionUID = 3436477890959388499L;
    
    /**
     *  ( )
     */
    private Integer code = ResultCodeEnum.SUCCESS.code();

    /**
     *  
     */
    private T data;

    /**
     *  
     */
    private String msg = "";

    /**
     *  
     */
    private String error = "";

    public static ResultUtil success = new ResultUtil();
    public static ResultUtil fail = new ResultUtil().code(ResultCodeEnum.FAIL.code());

    //  
    public static ResultUtil build() {
        return new ResultUtil();
    }

    public static  ResultUtil build(T data) {
        return new ResultUtil().data(data);
    }

    //  
    public ResultUtil data(T data) {
        this.data = data;
        return this;
    }

    public ResultUtil code(Integer code) {
        this.code = code;
        return this;
    }

    public ResultUtil msg(String msg) {
        this.msg = msg;
        return this;
    }

    public ResultUtil error(String error) {
        this.error = error;
        return this;
    }

 /*   public ResultUtil getXxx(){
        Xxx xxx = new Xxx();
        return ResultUtil.build(xxx);
        // return ResultUtil.success.data(xxx).msg(" ");
        // return ResultUtil.fail.error(" !");
        // return ResultUtil.fail.code(ResultCodeEnum.FAIL.code()).error(" !");
        // return ResultUtil.fail.code(ResultCodeEnum.FAIL.code()).error(ResultCodeEnum.FAIL.msg());
    }*/
}