springboot全局异常处理,不会去拦截sql异常,导致错误返回到前端,,将sql中的字段暴露出来,很危险,,
因为ExceptionHandler 只认我们注入类的名称,,而SQLException
不在里面,,
在RuntimeException中拦截:
@ExceptionHandler(RuntimeException.class)
public AjaxResult handleRuntimeException(RuntimeException e, HttpServletRequest request)
{
String requestURI = request.getRequestURI();
log.error("请求地址'{}',发生未知异常.", requestURI, e);
Throwable rootCause = e.getCause();
if (rootCause instanceof SQLIntegrityConstraintViolationException){
return AjaxResult.error("数据不唯一");
}
if (rootCause.toString().startsWith("org.mariadb.jdbc")){
return AjaxResult.error("数据库错误");
}
return AjaxResult.error(e.getMessage());
}
引用:https://download.csdn.net/blog/column/12507643/134327833