一、使用到的注解:
@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;
@RestControllerAdvice
public class ProjectExceptionAdvice {
@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) {
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";
六、项目异常的处理
项目异常分类:
业务异常:
规范的用户行为产生的异常
不规范的用户行为操作产生的异常
系统异常
项目运行过程中可预计且无法避免的异常
其他异常
编程人员未预期到的异常
异常处理方案
业务异常:
发送对应消息传递给用户,提醒规范操作
系统异常:
发送固定消息传递给用户,安抚用户
其他异常:
发送固定消息传递给用户,安抚用户