石家庄建站,六安马启兵胡冰倩婚礼,内蒙古网站备案怎么做,网络推广公司深圳文章目录 Mabatis处理异常屏蔽SQL返回前端全局异常捕获处理结论1 java异常体系2 Spring框架异常处理3 定位Spring框架转化为哪种unchecked异常3.1 捕获RuntimeException定位Spring框架转化抛出的异常类3.2 进一步查看包名判断3.3 识别MyBatisSystemException下级实现3.3 识别My… 文章目录 Mabatis处理异常屏蔽SQL返回前端全局异常捕获处理结论1 java异常体系2 Spring框架异常处理3 定位Spring框架转化为哪种unchecked异常3.1 捕获RuntimeException定位Spring框架转化抛出的异常类3.2 进一步查看包名判断3.3 识别MyBatisSystemException下级实现3.3 识别MyBatisSystemException继承实现 Mabatis处理异常屏蔽SQL返回前端全局异常捕获处理
结论
在全局异常处理类中添加MyBatisSystemException即可单独对MyBatis中和数据库操作相关异常操作进行全局处理同时屏蔽sql内容只返回文字 “服务错误请联系系统管理员” 给前端。
Slf4j
ControllerAdvice
public class ExceptionHandlerAdvice {/*** Sql查询失败在spring的包装下会统一抛出非受检异常,单独捕获,防止sql语句被返回给前端*/ResponseBodyExceptionHandler(MyBatisSystemException.class)public Object handleBindException(HttpServletRequest req, MyBatisSystemException e) {String path http://req.getRemoteAddr():req.getServerPort() req.getRequestURI();log.error(访问 path 报错,报错信息为: e.getMessage(), e);return new BaseResult(CodeEnum.E500, false, 服务错误请联系系统管理员。);}//拦截所有Exception,展示Error页面ResponseBodyExceptionHandler({Exception.class})public BaseResult errorHandler(HttpServletRequest req, Exception e) {String path http://req.getRemoteAddr():req.getServerPort() req.getRequestURI();log.error(访问 path 报错,报错信息为: e.getMessage(), e);return new BaseResult(CodeEnum.E500, false, e.getMessage());}
}1 java异常体系 1.Throwable
所有的异常都是Throwable的直接或者间接子类。Throwable有两个直接子类Error和Exception。
2.Error
Error是错误对于所有的编译时期的错误以及系统错误都是通过Error抛出的。这些错误表示故障发生于虚拟机自身、或者发生在虚拟机试图执行应用时如Java虚拟机运行错误Virtual MachineError、类定义错误NoClassDefFoundError等。这些错误是不可查的因为它们在应用程序的控制和处理能力之 外而且绝大多数是程序运行时不允许出现的状况。对于设计合理的应用程序来说即使确实发生了错误本质上也不应该试图去处理它所引起的异常状况。在 Java中错误通过Error的子类描述。
3.Exception
它规定的异常是程序本身可以处理的异常。异常和错误的区别是异常是可以被处理的而错误是没法处理的。
4.Checked Exception【受检异常】
可检查的异常这是编码时非常常用的所有checked exception都是需要在代码中处理的。它们的发生是可以预测的正常的一种情况可以合理的处理。例如IOException。
5.Unchecked Exception【非受检异常】
RuntimeException及其子类都是unchecked exception。比如NPE空指针异常除数为0的算数异常ArithmeticException等等这种异常是运行时发生无法预先捕捉处理的。Error也是unchecked exception也是无法预先处理的。
参考https://juejin.cn/post/6965407291260534820
2 Spring框架异常处理
Spring 提供方便的 API 把具体技术相关的异常(比如由JDBOHibernate or JDO 抛出的)转化为一致的 unchecked 异常。
3 定位Spring框架转化为哪种unchecked异常
3.1 捕获RuntimeException定位Spring框架转化抛出的异常类
直接在ExceptionHandlerAdvice中捕获RuntimeException然后DEBUG查看异常class类型发现都是继承自MyBatisSystemException 3.2 进一步查看包名判断
进一步查看包名发现为org.springframework.dao基本可以判定捕获MyBatisSystemException可以实现要求
package org.mybatis.spring;import org.springframework.dao.UncategorizedDataAccessException;public class MyBatisSystemException extends UncategorizedDataAccessException {private static final long serialVersionUID -5284728621670758939L;public MyBatisSystemException(Throwable cause) {super((String)null, cause);}
}3.3 识别MyBatisSystemException下级实现
MyBatisSystemException目前没有下级实现类
3.3 识别MyBatisSystemException继承实现
可以看到继承父类均为abstract修饰一直到NestedRuntimeException继承RuntimeException。则已经找到MyBatisSystemException的所有上级继承父类进一步确认MyBatisSystemException符合作为全局异常捕获ExceptionHandler的最上级实现异常类型而不会漏异常捕获。
package org.springframework.dao;import org.springframework.lang.Nullable;public abstract class UncategorizedDataAccessException extends NonTransientDataAccessException {public UncategorizedDataAccessException(Nullable String msg, Nullable Throwable cause) {super(msg, cause);}
}package org.springframework.dao;import org.springframework.lang.Nullable;public abstract class NonTransientDataAccessException extends DataAccessException {public NonTransientDataAccessException(String msg) {super(msg);}public NonTransientDataAccessException(Nullable String msg, Nullable Throwable cause) {super(msg, cause);}
}package org.springframework.dao;import org.springframework.core.NestedRuntimeException;
import org.springframework.lang.Nullable;public abstract class DataAccessException extends NestedRuntimeException {public DataAccessException(String msg) {super(msg);}public DataAccessException(Nullable String msg, Nullable Throwable cause) {super(msg, cause);}
}package org.springframework.core;import org.springframework.lang.Nullable;public abstract class NestedRuntimeException extends RuntimeException {private static final long serialVersionUID 5439915454935047936L;public NestedRuntimeException(String msg) {super(msg);}public NestedRuntimeException(Nullable String msg, Nullable Throwable cause) {super(msg, cause);}Nullablepublic String getMessage() {return NestedExceptionUtils.buildMessage(super.getMessage(), this.getCause());}Nullablepublic Throwable getRootCause() {return NestedExceptionUtils.getRootCause(this);}public Throwable getMostSpecificCause() {Throwable rootCause this.getRootCause();return (Throwable)(rootCause ! null ? rootCause : this);}public boolean contains(Nullable Class? exType) {if (exType null) {return false;} else if (exType.isInstance(this)) {return true;} else {Throwable cause this.getCause();if (cause this) {return false;} else if (cause instanceof NestedRuntimeException) {return ((NestedRuntimeException)cause).contains(exType);} else {while(cause ! null) {if (exType.isInstance(cause)) {return true;}if (cause.getCause() cause) {break;}cause cause.getCause();}return false;}}}static {NestedExceptionUtils.class.getName();}
}}cause cause.getCause();}return false;}}}static {NestedExceptionUtils.class.getName();}
}