代碼:
import com.xx.xx.admin.model.JsonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
/**
-
全局統一異常處理
-
@Author zzq
-
@Date 2020/12/25 10:01
*/
@ControllerAdvice
public class GlobalExceptionHandler {
private final static Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);/**
- 功能描述:異常處理
- @param e 異常參數
- @return R 返回給前端的異常信息
*/
@ExceptionHandler(Exception.class)
@ResponseBody
public JsonResult error(Exception e) {
logger.error(String.format("統一異常攔截:%s", e.getMessage()));
return JsonResult.error(Constant.RESPONSE_FAIL, "請求失敗");
}
}
其實主要就是 ControllerAdvice 注解,ControllerAdvice 注解就是監控全局異常的一個注解