使用RestControllerAdvice做全域的例外控制,回傳我自定義的一個物件。
使用debug模式步進,出現錯誤的時候能夠正常的進入RestControllerAdvice注解下的類中對應ExceptionHandler注解的方法,也回傳了我自定義的物件,但是在最后的回傳結果中,并不是我自定義的這個物件,而是springmvc的默認錯誤,Internal Server Error。
上網查了下,得到的答案基本都是錯誤沒正常拋出啊,切面中進行了限制啊什么的,但錯誤肯定是正常拋出了,要不不會進入到那個例外控制中。切面也只是一些鑒權啊什么的,我也試過把aop都停用掉,但還是不行。
uj5u.com熱心網友回復:
/**
* @author liuyh
* @Description:
* @date 2020/9/2214:13
*/
@ControllerAdvice
@Slf4j
public class ExceptionError {
@ExceptionHandler(value = Exception.class)
@ResponseBody
public Result defaultErrorHandler(HttpServletRequest req, HttpServletResponse resp, Exception e) {
e.printStackTrace();
Result resul = new Result();
if (e instanceof HttpRequestMethodNotSupportedException) {
resul.setResltInfo(Constant.CODE_TYPE_10, "非法請求", e.getMessage());
} else if (e instanceof HystrixRuntimeException) {
resul.setResltInfo(Constant.CODE_TYPE_30, "feign呼叫系統例外", e.getCause().getMessage());
} else {
resul.setResltInfo(Constant.CODE_TYPE_1, "系統例外", e.getMessage());
}
return resul;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/212871.html
標籤:Java SE
上一篇:時間序列特征分析匯總(以2012-2019年槽罐車事故資料為例)
下一篇:c語言鏈表及其基本操作
