前言
軟體開發springboot專案程序中,不可避免的需要處理各種例外,spring mvc 架構中各層會出現大量的try {...} catch {...} finally {...} 代碼塊,不僅有大量的冗余代碼,而且還影響代碼的可讀性,這樣就需要定義個全域統一例外處理器,以便業務層再也不必處理例外,
推薦理由
-
代碼復制到專案中通過簡單的配置即可實作
-
可以靈活的根據自己的業務例外進行更細粒度的擴展
實踐
1.封裝統一回傳結果類
public class AjaxResult {
//是否成功
private Boolean success;
//狀態碼
private Integer code;
//提示資訊
private String msg;
//資料
private Object data;
public AjaxResult() {
}
//自定義回傳結果的構造方法
public AjaxResult(Boolean success,Integer code, String msg,Object data) {
this.success = success;
this.code = code;
this.msg = msg;
this.data = https://www.cnblogs.com/bainannan/p/data;
}
//自定義例外回傳的結果
public static AjaxResult defineError(BusinessException de){
AjaxResult result = new AjaxResult();
result.setSuccess(false);
result.setCode(de.getErrorCode());
result.setMsg(de.getErrorMsg());
result.setData(null);
return result;
}
//其他例外處理方法回傳的結果
public static AjaxResult otherError(ErrorEnum errorEnum){
AjaxResult result = new AjaxResult();
result.setMsg(errorEnum.getErrorMsg());
result.setCode(errorEnum.getErrorCode());
result.setSuccess(false);
result.setData(null);
return result;
}
public Boolean getSuccess() {
return success;
}
public void setSuccess(Boolean success) {
this.success = success;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}
2 自定義例外封裝類
public class BusinessException extends RuntimeException {
private static final long serialVersionUID = 1L;
/**
* 錯誤狀態碼
*/
protected Integer errorCode;
/**
* 錯誤提示
*/
protected String errorMsg;
public BusinessException(){
}
public BusinessException(Integer errorCode, String errorMsg) {
this.errorCode = errorCode;
this.errorMsg = errorMsg;
}
public Integer getErrorCode() {
return errorCode;
}
public void setErrorCode(Integer errorCode) {
this.errorCode = errorCode;
}
public String getErrorMsg() {
return errorMsg;
}
public void setErrorMsg(String errorMsg) {
this.errorMsg = errorMsg;
}
}
3 錯誤列舉,拒絕硬編碼
public enum ErrorEnum {
// 資料操作錯誤定義
SUCCESS(200, "成功"),
NO_PERMISSION(403,"你沒得權限"),
NO_AUTH(401,"未登錄"),
NOT_FOUND(404, "未找到該資源!"),
INTERNAL_SERVER_ERROR(500, "服務器例外請聯系管理員"),
;
/** 錯誤碼 */
private Integer errorCode;
/** 錯誤資訊 */
private String errorMsg;
ErrorEnum(Integer errorCode, String errorMsg) {
this.errorCode = errorCode;
this.errorMsg = errorMsg;
}
public Integer getErrorCode() {
return errorCode;
}
public String getErrorMsg() {
return errorMsg;
}
}
4 全域例外處理類
/**
* 全域例外處理器
*
*/
@RestControllerAdvice
public class GlobalExceptionHandler
{
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
/**
* 處理自定義例外
*
*/
@ExceptionHandler(value = https://www.cnblogs.com/bainannan/p/BusinessException.class)
public AjaxResult bizExceptionHandler(BusinessException e) {
log.error(e.getMessage(), e);
return AjaxResult.defineError(e);
}
/**
* 處理其他例外
*
*/
@ExceptionHandler(value = Exception.class)
public AjaxResult exceptionHandler( Exception e) {
log.error(e.getMessage(), e);
return AjaxResult.otherError(ErrorEnum.INTERNAL_SERVER_ERROR);
}
}
5 測驗
回傳結果:
總結:
小編總結了2020面試題,這份面試題的包含的模塊分為19個模塊,分別是: Java 基礎、容器、多執行緒、反射、物件拷貝、Java Web 、例外、網路、設計模式、Spring/Spring MVC、Spring Boot/Spring Cloud、Hibernate、MyBatis、RabbitMQ、Kafka、Zookeeper、MySQL、Redis、JVM ,
關注我的公眾號:程式員白楠楠,獲取上述資料,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/235863.html
標籤:Java
上一篇:網路 IO 模型簡單介紹
下一篇:Java基礎之:OOP——內部類
