SpringMVC設(shè)置全局異常處理器的步驟
背景
在項目中我們有需求做一個全局異常處理,來規(guī)范所有出去的異常信息。
參考:官方文檔
分析
首先 ControllerAdvice(RestControllerAdvice ) ,ControllerAdvice 是無法處理過濾器和攔截器中的異常的。
引用一張圖

下面介紹controller層的全局異常設(shè)置
全局異常處理也有多種方式
使用@ControllerAdvice(@RestControllerAdvice)+@ExceptionHandler實現(xiàn)全局異常
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {
/**
* 處理參數(shù)錯誤的異常
* @param e
* @return
*/
@ResponseBody
@ExceptionHandler(value = IllegalParamsException.class)
public ResultVO<Object> handleIllegalParamsException(IllegalParamsException e) {
ResultVO<Object> resultVo = new ResultVO<>();
resultVo.setStatus(HttpStatus.BAD_REQUEST.value());
resultVo.setErrorCode(e.getErrorInfo().getErrorCode());
resultVo.setErrorMsg(e.getErrorInfo().getErrorDesc());
return resultVo;
}
@ResponseBody
@ExceptionHandler(value = Exception.class)
public ResultVO<Object> handleException(Exception e) {
ResultVO<Object> resultVo = new ResultVO<>();
resultVo.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
resultVo.setErrorMsg(e.getMessage());
return resultVo;
}
}@Data
@AllArgsConstructor
@NoArgsConstructor
public class ResultVO<T> {
private Integer status;
private String errorCode;
private String errorMsg;
private T data;
public ResultVO(Integer status, String errorCode, String errorMsg) {
this.status = status;
this.errorCode = errorCode;
this.errorMsg = errorMsg;
}
}public class IllegalParamsException extends RuntimeException {
private static final long serialVersionUID = -6298406656682893468L;
private OperationErrorEnum errorInfo;
public IllegalParamsException(OperationErrorEnum errorInfo) {
this.errorInfo = errorInfo;
}
public IllegalParamsException(String message, OperationErrorEnum errorInfo) {
super(message);
this.errorInfo = errorInfo;
}
public IllegalParamsException(String message, Throwable cause, OperationErrorEnum errorInfo) {
super(message, cause);
this.errorInfo = errorInfo;
}
public IllegalParamsException(Throwable cause, OperationErrorEnum errorInfo) {
super(cause);
this.errorInfo = errorInfo;
}
public IllegalParamsException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace, OperationErrorEnum errorInfo) {
super(message, cause, enableSuppression, writableStackTrace);
this.errorInfo = errorInfo;
}
public OperationErrorEnum getErrorInfo() {
return errorInfo;
}
}全局異常處理-多個處理器匹配順序
參考:參考
多個處理器的兩種情況:
存在一個類中
子類異常處理器優(yōu)先
存在不同的類中
與多個異常處理類放入LinkedHashMap的順序有關(guān),
可以利用Order指定順序,如果沒有,則默認最小順序;
那么,如果都沒有指定順序的話,那就是list中的順序
對于過濾器和攔截器中的異常,有兩種思路可以考慮
1、catch后通過轉(zhuǎn)發(fā)到異常頁面(設(shè)置ModelAndView)
參考:參考
2、攔截器中發(fā)生異常,攔截器中直接返回錯誤(通過response.getOutputStream().write() 直接寫錯誤信息)
如:
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
try {
// 業(yè)務(wù)代碼
} catch (Exception e) {
response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
ResultVO<Object> resultVo = new ResultVO<>();
resultVo.setStatus(HttpStatus.UNAUTHORIZED.value());
resultVo.setErrorMsg(ACCESS_PARAM_ERROR.getErrorDesc());
response.getOutputStream().write(new String(JSON.toJSONString(resultVo)).getBytes(StandardCharsets.UTF_8));
logger.error("==== WhiteListAndAuthenticationInterceptor攔截器攔截到了方法:{} 解析鑒權(quán)參數(shù)異常 ====", methodName);
return false;
}
}到此這篇關(guān)于SpringMVC設(shè)置全局異常處理器的步驟的文章就介紹到這了,更多相關(guān)SpringMVC全局異常處理器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java計時器StopWatch實現(xiàn)方法代碼實例
這篇文章主要介紹了Java計時器StopWatch實現(xiàn)方法代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-07-07
Java基礎(chǔ)之選擇結(jié)構(gòu)與循環(huán)結(jié)構(gòu)
這篇文章主要介紹了Java基礎(chǔ)之選擇結(jié)構(gòu)與循環(huán)結(jié)構(gòu),文中有非常詳細的代碼示例,對正在學(xué)習(xí)java基礎(chǔ)的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04
Java8接口中引入default關(guān)鍵字的本質(zhì)原因詳析
Default方法是在java8中引入的關(guān)鍵字,也可稱為Virtual extension methods—虛擬擴展方法,這篇文章主要給大家介紹了關(guān)于Java8接口中引入default關(guān)鍵字的本質(zhì)原因,需要的朋友可以參考下2022-01-01

