开发小技巧——自定义异常、统一返回实体写法

自定义异常

package com.ccssoft.soft.exception;

/**
* @date: 2018/6/14
* @description: 自定义异常
*/
public class CheckException extends RuntimeException {

    public CheckException() { }

    public CheckException(String message) {
        super(message);
    }

    public CheckException(Throwable cause) {
        super(cause);
    }

    public CheckException(String message, Throwable cause) {
        super(message, cause);
    }

    protected CheckException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
    }
}

统一返回实体

package com.ccssoft.soft.model;

import lombok.Data;

import java.io.Serializable;

/**
* @date: 2018/6/14
* @description: 统一返回实体
*/
@Data
public class ResultModel implements Serializable {

    public static final int SUCCESS = 0;
    public static final int FAIL = 1;
    public static final int NO_PERMISSION = 2;

    // 提示信息
    private String msg = "success";

    // 状态值
    private int code = SUCCESS;

    // 具体实体
    private T data;

    public ResultModel() {
        super();
    }

    public ResultModel(T data) {
        super();
        this.data = data;
    }

    public ResultModel(Throwable e) {
        super();
        this.msg = e.getMessage();
        this.code = FAIL;
    }

}

 

你可能感兴趣的:(Java开发)