springMVC 全局异常处理

@RestControllerAdvice //控制器增强
public class GlobalException {

    //捕捉sql类的异常,可以改为想要捕捉的任何异常
    @ExceptionHandler(SQLException.class)
    public RespBean mySqlException(SQLException e) {
        if (e instanceof SQLIntegrityConstraintViolationException){
            return RespBean.error("该数据有关联数据,操作失败");
        }
        return RespBean.error("数据库异常,操作失败");
    }

  //其他方法。。。。 }

 

posted @ 2022-06-04 20:30  黄大虾  阅读(26)  评论(0编辑  收藏  举报