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