springmvc高可用響應物件以及全域性異常處理類的編寫
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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- .netcore全域性異常處理NetCore
- springboot全域性異常處理Spring Boot
- SpringBoot中的全域性異常處理Spring Boot
- 【SpringBoot】全域性異常處理@ControllerAdviceSpring BootController
- NETCORE - 全域性異常處理(Exception)NetCoreException
- SpringBoot之全域性異常處理Spring Boot
- SpringBoot優雅的全域性異常處理Spring Boot
- Spring Cloud Gateway的全域性異常處理SpringCloudGateway
- springboot下新增全域性異常處理和自定義異常處理Spring Boot
- SpringMVC異常處理SpringMVC
- dotNet8 全域性異常處理
- SpringBoot處理全域性統一異常Spring Boot
- 簡單的全域性異常統一處理
- Spring Boot 2 Webflux的全域性異常處理Spring BootWebUX
- 【SpringMVC】 4.2 異常處理SpringMVC
- 重寫Laravel異常處理類Laravel
- SpringBoot進行優雅的全域性異常處理Spring Boot
- SpringMVC異常的處理機制SpringMVC
- @ControllerAdvice 全域性異常響應頁面和 JSONControllerJSON
- spring-boot-route(四)全域性異常處理Springboot
- 設計一個全域性異常處理器
- SpringMVC 統一異常處理SpringMVC
- Django筆記三十一之全域性異常處理Django筆記
- Vue3 如何實現全域性異常處理?Vue
- Day133.異常處理 -SpringMVCSpringMVC
- 如何使用SpringMvc處理Rest異常SpringMVCREST
- 聊聊springboot專案全域性異常處理那些事兒Spring Boot
- SpringBoot錯誤處理機制以及自定義異常響應原理解析Spring Boot
- SpringMVC處理請求頭、響應頭、編碼行為SpringMVC
- Laravel 打造適合 API 的異常處理響應格式LaravelAPI
- SpringMVC學習系列(10) 之 異常處理SpringMVC
- Spring Boot乾貨系列:(十三)Spring Boot全域性異常處理整理Spring Boot
- SpringMVC學習筆記10-異常處理SpringMVC筆記
- NetCore實現全域性模型繫結異常資訊統一處理NetCore模型
- spring boot 實現監聽器、過濾器、全域性異常處理Spring Boot過濾器
- 實戰Spring Boot 2.0系列(二) - 全域性異常處理和測試Spring Boot
- 記錄Laravel異常處理類Laravel
- python應用:異常處理Python