JAVA_Spring_SpringMVC异常处理

一、使用到的注解:

@RestControllerAdvice:代表这是rest风格的Controller,告诉springmvc这是异常处理器
@ExceptionHandler:用于拦截异常,可在括号中标明拦截何种异常
                  该注解的位置:专用于异常处理器的上方
                  作用:设置指定异常的处理方案,功能等同于控制器方法,出现异常后终止原始控制器执行,并转入当前方法
                  意思是浏览器在请求过程中发生在表现层的异常都会被在这里捕捉处理,从异常处理器返回给浏览器信息
                  说明:此类方法可以根据处理的异常不同,制作多个方法分别处理对应的异常
                  告诉springmvc这个方法处理什么样的异常

二、自定义异常,给可以预料到的异常使用

2.1系统异常

SystemException.java:
//自定义:系统异常
public class SystemException extends RuntimeException{
//    重写构造方法,用来自定义异常
//    给一个编号,便于区分是什么异常
    private Integer code;

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public SystemException(String message, Integer code) {
        super(message);
        this.code = code;
    }

    public SystemException(String message, Throwable cause, Integer code) {
        super(message, cause);
        this.code = code;


//    下面这些,没用到就可以放着不动
    public BusinessException(Integer code) {
        this.code = code;
    }

    public BusinessException(Throwable cause, Integer code) {
        super(cause);
        this.code = code;
    }

    public BusinessException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace, Integer code) {
        super(message, cause, enableSuppression, writableStackTrace);
        this.code = code;
    }
    }

2.2业务层异常:

BusinessException.java:
//自定义:业务层异常
public class BusinessException extends RuntimeException{
    //    重写构造方法,用来自定义异常
//    给一个编号,便于区分是什么异常
    private Integer code;

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }


    public BusinessException(String message, Integer code) {
        super(message);
        this.code = code;
    }

    public BusinessException(String message, Throwable cause, Integer code) {
        super(message, cause);
        this.code = code;
    }







    //    下面这些,没用到就可以放着不动
    public BusinessException(Integer code) {
        this.code = code;
    }

    public BusinessException(Throwable cause, Integer code) {
        super(cause);
        this.code = code;
    }

    public BusinessException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace, Integer code) {
        super(message, cause, enableSuppression, writableStackTrace);
        this.code = code;
    }
}

三、创建controller层的异常处理器(重点),来使用自定义异常

ProjectExceptionAdvice.java
import cn.itheima.controller.controllerUtil.Code;
import cn.itheima.controller.controllerUtil.Result;
import cn.itheima.exception.BusinessException;
import cn.itheima.exception.SystemException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

//处理表现层(controller)的异常
@RestControllerAdvice//告诉springmvc这是异常处理器
public class ProjectExceptionAdvice {
    //    该注解的位置:专用于异常处理器的上方
//    作用:设置指定异常的处理方案,功能等同于控制器方法,出现异常后终止原始控制器执行,并转入当前方法
//    意思是浏览器在请求过程中发生在表现层的异常都会被在这里捕捉处理,从异常处理器返回给浏览器信息
//    说明:此类方法可以根据处理的异常不同,制作多个方法分别处理对应的异常
//    告诉springmvc这个方法处理什么样的异常
    @ExceptionHandler(Exception.class)
//    这个异常是为了记录未知的异常
    public Result doException(Exception ex) {
        //        记录日志
//        发送消息给运维
//        发送有邮件给开发人员
        System.out.println("已捕捉到异常");
        return new Result(Code.SYSTEM_UNKNOW_ERR, "已捕捉到异常");
    }

    //        拦截自定义系统异常
    @ExceptionHandler(SystemException.class)
    public Result doSystemException(SystemException ex) {
//        记录日志
//        发送消息给运维
//        发送有邮件给开发人员
//        返回给前端
        return new Result(null, String.valueOf(ex.getCode()), ex.getMessage());

    }

    //    拦截自定义业务异常
    @ExceptionHandler(BusinessException.class)
    public Result doBusinessException(BusinessException ex) {
        return new Result(null, String.valueOf(ex.getCode()), ex.getMessage());
    }

}

四、在业务层(Service)中模拟一下异常

// 测试异常处理器的方法
    @Override
    public Users selectById(Integer id) {
//        编写一个业务层异常
//传的id值为1就是业务层异常
        if(id == 1){
            throw new BusinessException("请不要乱传参数", Code.BUSINESS_ERR);
        }

//将可能出现的异常进行包装,转换成自定义系统异常异常
        try {
            int i = 1/0;
        } catch (ArithmeticException aex) {
//            自定义的系统异常,这就将异常传到了表现成
            throw new SystemException("服务器超时,请重试",aex,Code.SYSTEM_TIMEOUT_ERR);
        }
        return userDao.selectById(id);
    }

五、为了可以更好的区分异常,给异常一个编码

/*
     * 异常使用的状态码
     * */
    Integer SYSTEM_ERR = 5001;
    Integer SYSTEM_TIMEOUT_ERR = 5002;
    Integer BUSINESS_ERR = 6001;
    String SYSTEM_UNKNOW_ERR = "5999";

六、项目异常的处理

项目异常分类:
业务异常:
      规范的用户行为产生的异常
      不规范的用户行为操作产生的异常
系统异常
      项目运行过程中可预计且无法避免的异常
其他异常
      编程人员未预期到的异常
异常处理方案
 业务异常:
      发送对应消息传递给用户,提醒规范操作
 系统异常:
      发送固定消息传递给用户,安抚用户
 其他异常:
      发送固定消息传递给用户,安抚用户
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值