ssm整合-项目异常处理方案
项目异常分类:
项目异常处理方案:
需要自定义异常处理
然后在处理器中加入
package com.itheima.controller;
import com.itheima.exception.BusinessException;
import com.itheima.exception.SystemException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice
public class ProjectExceptionAdvice {
@ExceptionHandler(BusinessException.class)
public Result doBusinessException(BusinessException ex){
return new Result(ex.getMessage(),null,ex.hashCode());
}
@ExceptionHandler(SystemException.class)
public Result doSystemException(SystemException ex){
//记录日志
//发送消息给运维
//发送邮件给开发人员
return new Result(ex.getMessage(),null,ex.hashCode());
}
@ExceptionHandler(Exception.class)
public Result doException(Exception ex){
return new Result("系统繁忙,请稍后再试", 666, Code.SYSTEM_UNKNOW_ERR);
}
}
把表现层可能出现的异常给抛出去