springmvc高可用響應物件以及全域性異常處理類的編寫

neuyu發表於2021-09-09

1,ResultModel響應類

public class ResultModel<T> {
    private Integer code = SUCCESS.getCode();
    private T data;
    private String requestId;
    private String resultMsg = SUCCESS.getDesc();
    private String errorStackTrace;

    private ResultModel() {
    }

    private static ResultModel SUCCESS_RESULT_MODEL = ResultModel.success(null);

    public static ResultModel success() {

        return SUCCESS_RESULT_MODEL;
    }

    @JsonIgnore
    public boolean isSuccess() {
        return code.equals(SUCCESS.getCode());
    }

    public static <T> ResultModel<T> success(T data) {
        return success(data, SUCCESS.getDesc());
    }

    public static <T> ResultModel<T> success(T data, String msg) {
        return new ResultModel<>(SUCCESS.getCode(), data, msg, null);
    }

    public static <T> ResultModel<T> fail(CommonException ex) {
        String resultMsg = ex.getError().getDesc();
        if (StringUtils.isNotEmpty(ex.getErrMsg())) {
            resultMsg = ex.getErrMsg();
        }
        return new ResultModel<>(ex.getError().getCode(), null, resultMsg, ex.getMessage());
    }

    public static ResultModel<Object> fail(CommonException ex, Object data) {
        String resultMsg = ex.getError().getDesc();
        if (StringUtils.isNotEmpty(ex.getErrMsg())) {
            resultMsg = ex.getErrMsg();
        }
        return new ResultModel<>(ex.getError().getCode(), data, resultMsg, ex.getMessage());
    }

    public static ResultModel<Object> fail(CommonErrorCode error) {
        return new ResultModel<>(error.getCode(), null, error.getDesc(), null);
    }

    public static ResultModel<Object> fail(CommonErrorCode error, String msg) {
        return new ResultModel<>(error.getCode(), null, msg, null);
    }

    public static ResultModel<Object> fail(CommonErrorCode error, Object data, String msg) {
        return new ResultModel<>(error.getCode(), data, msg, null);
    }

    public static ResultModel<Object> fail(CommonErrorCode error, Object data, Throwable ex) {
        return fail(error, data, error.getDesc(), ex);
    }

    public static ResultModel<Object> fail(CommonErrorCode error, Throwable ex) {
        return new ResultModel<>(error.getCode(), null, error.getDesc(), ex.getMessage());
    }

    public static ResultModel<Object> fail(CommonErrorCode error, String msg, Throwable ex) {
        return fail(error, null, msg, ex);
    }

    public static ResultModel<Object> fail(CommonErrorCode error, Object data, String msg, Throwable ex) {
        return new ResultModel<>(error.getCode(), data, msg, ex.getMessage());
    }

    public ResultModel(int code, T data, String resultMsg, String errorStackTrace) {
        this.code = code;
        this.data = data;
        this.resultMsg = resultMsg;
        this.errorStackTrace = errorStackTrace;
        this.requestId = MDC.get("MDC_LOG_ID");
    }

    public Integer getCode() {
        return code;
    }

    public String getErrorStackTrace() {
        return errorStackTrace;
    }

    public String getResultMsg() {
        return resultMsg;
    }

    public T getData() {
        return data;
    }

    public String getRequestId() {
        return requestId;
    }

    public void setRequestId(String requestId) {
        this.requestId = requestId;
    }
}

2,CommonErrorCode錯誤碼

public enum CommonErrorCode {
    SUCCESS(200, "執行成功"),

    USER_NOT_LOGIN(400, "使用者未登入"),
    VALIDATE_ERROR(401, "校驗資料錯誤"),
    NOT_EXIST(404, "不存在"),
    FORBIDDEN(403, "許可權不足"),
    RESOURCE_NOT_FOUND(404, "資源未找到"),
    RESOURCE_CONFLICT(409, "資源衝突"),
    REQUEST_PARAM_ERROR(412, "引數錯誤"),
    PRECONDITION_FAILED(428, "要求先決條件"),
    NOT_SUPPORT(409, "不支援的請求"),
    WRONG_USERNAME_OR_PASSWORD(490, "使用者名稱或密碼錯誤"),
    WRONG_OPEN_API_AUTHORITY(491, "認證錯誤"),

    UNKNOWN_ERROR(500, "系統未知錯誤"),
    SERVER_POWER_LESS(501, "伺服器無法完成該請求"),
    RETRY_ERROR(502, "伺服器重試出錯"),

    OPTIMISTIC_LOCK_VERSION_ERROR(600, "樂觀鎖版本錯誤");

    /**
     * 錯誤碼
     */
    private Integer code;
    /**
     * 錯誤描述
     */
    private String desc;

    CommonErrorCode(Integer code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    public Integer getCode() {
        return code;
    }

    public String getDesc() {
        return desc;
    }
}

3,CommonException全域性異常處理類

public class CommonException extends RuntimeException {

    protected String errMsg;            // 錯誤提示資訊,顯示給使用者
    protected String detailMsg;         // 錯誤的具體資訊,可能包含一些引數ID等資訊
    protected CommonErrorCode error;    // 錯誤碼
    protected Object data;              // 返回內容

    public CommonException(CommonErrorCode error) {
        super(error.getDesc());
        this.error = error;
        this.errMsg = error.getDesc();
        this.detailMsg = error.getDesc();
    }

    public CommonException(CommonErrorCode error, String errMsg) {
        super(errMsg);
        this.error = error;
        this.errMsg = errMsg;
        this.detailMsg = errMsg;
    }

    public CommonException(CommonErrorCode error, String errMsg, String detailMsg) {
        super(StringUtils.isEmpty(detailMsg) ? errMsg : detailMsg);
        this.error = error;
        this.errMsg = errMsg;
        this.detailMsg = detailMsg;
    }

    public CommonException(CommonErrorCode error, String errMsg, Throwable cause) {
        super(errMsg, cause);
        this.error = error;
        this.errMsg = errMsg;
        this.detailMsg = errMsg;
    }

    public CommonException(CommonErrorCode error, String errMsg, String detailMsg, Throwable cause) {
        super(StringUtils.isEmpty(detailMsg) ? errMsg : detailMsg, cause);
        this.error = error;
        this.errMsg = errMsg;
        this.detailMsg = detailMsg;
    }

    public CommonException(CommonErrorCode error, Throwable cause) {
        super(error.getDesc(), cause);
        this.error = error;
        this.errMsg = error.getDesc();
        this.detailMsg = error.getDesc();
    }

    public String getErrMsg() {
        return errMsg;
    }

    public CommonErrorCode getError() {
        return error;
    }

    public String getDetailMsg() {
        return detailMsg;
    }

    public void setDetailMsg(String detailMsg) {
        this.detailMsg = detailMsg;
    }

    public void setErrMsg(String errMsg) {
        this.errMsg = errMsg;
    }

    public Object getData() {
        return data;
    }

    public CommonException setData(Object data) {
        this.data = data;
        return this;
    }
}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/4550/viewspace-2821261/,如需轉載,請註明出處,否則將追究法律責任。

相關文章