当前位置:网站首页>SSM整合-异常处理器及项目异常处理方案
SSM整合-异常处理器及项目异常处理方案
2022-07-02 12:46:00 【夏志121】
目录
一、异常处理器
程序开发过程中不可避免的会遇到异常现象
出现异常现象的常见位置与常见诱因如下:
■ 框架内部抛出的异常:因使用不合规导致
■ 数据层抛出的异常:因外部服务器故障导致(例如:服务器访问超时)
■ 业务层抛出的异常:因业务逻辑书写错误导致(例如:遍历业务书写操作,导致索引异常等)
■ 表现层抛出的异常:因数据收集、校验等规则导致(例如:不匹配的数据类型间导致异常)
■ 工具类抛出的异常:因工具类书写不严谨不够健壮导致(例如:必要释放的连接长期未释放等)
1、各个层级出现异常,异常处理代码书写在哪一层?
所有的异常均抛出到表现层进行处理
2、表现层处理异常,每个方法单独书写,代码书写量巨大且意义不强,如何解决?
AOP思想
异常处理器
集中的、统一的处理项目中出现的异常
@RestControllerAdvice
public class ProjectExceptionAdvice{
@ExceptionHandler(Exception.class)
public Result doException(Exception ex){
return new Result(666,null);
}
}
名称:@RestControllerAdvice
类型:类注解
位置:Rest风格开发的控制器增强类定义上方
作用:为Rest风格开发的控制器类做增强
范例:
@RestControllerAdvice public class ProjectExceptionAdvice{ }
说明:此注解自带@ResponseBody注解与@Component注解,具备对应的功能
名称:@ExceptionHandler
类型:方法注解
位置:专用于异常处理的控制器方法上方
作用:设置指定异常的处理方案,功能等同于控制器方法,出现异常后终止原始控制器执行,并转入当前方法执行
范例:
@RestControllerAdvice public class ProjectExceptionAdvice{ @ExceptionHandler(Exception.class) public Result doException(Exception ex){ return new Result(666,null); } }
说明:此类方法可以根据处理的异常不同,制作多个方法分别处理对应的异常
异常处理器处理效果比对:
无异常时:
有异常时:
二、项目异常处理方案
项目异常分类
业务异常(BusinessException)
规范的用户行为产生的异常
不规范的用户行为产生的异常
系统异常(SystemException)
项目运行过程中可预计且无法避免的异常
其他异常(Exception)
编程人员未预期到的异常
项目异常处理方案
业务异常(BusinessException)
发送对应消息传递给用户,提醒规范操作
系统异常(SystemException)
发送固定消息传递给用户,安抚用户
发送特定消息给运维人员,提醒维护
记录日志
其他异常(Exception)
发送固定消息传递给用户,安抚用户
发送特定消息给编程人员,提醒维护(纳入预期范围内)
记录日志
项目异常处理
1、自定义项目异常系统级异常
//自定义异常处理器,用于封装异常信息,对异常进行分类
public class SystemException extends RuntimeException{
private Integer code;
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public SystemException(Integer code, String message) {
super(message);
this.code = code;
}
public SystemException(Integer code, String message, Throwable cause) {
super(message, cause);
this.code = code;
}
}
2、自定义项目业务级异常
//自定义异常处理器,用于封装异常信息,对异常进行分类
public class BusinessException extends RuntimeException{
private Integer code;
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public BusinessException(Integer code, String message) {
super(message);
this.code = code;
}
public BusinessException(Integer code, String message, Throwable cause) {
super(message, cause);
this.code = code;
}
}
3、自定义异常编码(持续补充)
public class Code {
public static final Integer SYSTEM_UNKNOW_ERROR = 50001;
public static final Integer SYSTEM_TIMEOUT_ERROR = 50002;
public static final Integer PROJECT_VALIDATE_ERROR = 60001;
public static final Integer PROJECT_BUSINESS_ERROR = 60002;
}
4、触发自定义异常
@Service
public class BookServiceImpl implements BookService {
@Autowired
private BookDao bookDao;
public Book getById(Integer id) {
//模拟业务异常,包装成自定义异常
if(id < 0){
throw new BusinessException(Code.PROJECT_BUSINESS_ERROR,"请勿进行非法操作!");
}
return bookDao.getById(id);
}
}
5、拦截并处理异常
//@RestControllerAdvice用于标识当前类为REST风格对应的异常处理器
@RestControllerAdvice
public class ProjectExceptionAdvice {
//@ExceptionHandler用于设置当前处理器类对应的异常类型
@ExceptionHandler(SystemException.class)
public Result doSystemException(SystemException ex){
//记录日志
//发送消息给运维
//发送邮件给开发人员,ex对象发送给开发人员
return new Result(ex.getCode(),null,ex.getMessage());
}
@ExceptionHandler(BusinessException.class)
public Result doBusinessException(BusinessException ex){
return new Result(ex.getCode(),null,ex.getMessage());
}
//除了自定义的异常处理器,保留对Exception类型的异常处理,用于处理非预期的异常
@ExceptionHandler(Exception.class)
public Result doOtherException(Exception ex){
//记录日志
//发送消息给运维
//发送邮件给开发人员,ex对象发送给开发人员
return new Result(Code.SYSTEM_UNKNOW_ERROR,null,"系统繁忙,请联系管理员!");
}
}
6、异常处理器效果对比
边栏推荐
- idea 公共方法抽取快捷键
- 全方位解读服务网格(Service Mesh)的背景和概念
- The sea of stars hidden behind the nebula graph
- Conditions and solutions of deadlock
- /Bin/ld: cannot find -lxslt
- Does bone conduction earphone have external sound? Advantages of bone conduction earphones
- Original God 2.6 server download and installation tutorial
- 中科大脑知识图谱平台建设及业务实践
- Usage of group by
- The difference and usage of calloc, malloc and realloc functions
猜你喜欢
理想之光不灭
华为云服务器安装mysqlb for mysqld.service failed because the control process exited with error code.See “sys
JS learning notes - operators
微信v3native支付设置的结束时间处理办法
Tree binary search tree
图数据库|Nebula Graph v3.1.0 性能报告
Maui学习之路(三)--Winui3深入探讨
MySQL calculates the data within the longitude and latitude range
Traversal before, during and after binary tree
Set the background picture in the idea (ultra detailed)
随机推荐
Bean configuration override in boot
idea 公共方法抽取快捷键
Ant group's large-scale map computing system tugraph passed the national evaluation
Solve * * warning * *: your ApplicationContext is unlikely to start due to a @componentscan of the defau
Comment réaliser un graphique Nebula d'importation CSV hors ligne de niveau milliard
Solve the problem of base64encoder error
Song of cactus - throwing stones to ask the way (3)
/Bin/ld: cannot find -lpam
Wise target detection 23 - pytoch builds SSD target detection platform
Analysis of the difference between array and linked list
解决** WARNING ** : Your ApplicationContext is unlikely to start due to a @ComponentScan of the defau
Locate: cannot execute stat() `/var/lib/mlocate/mlocate Db ': there is no such file or directory
Introduction to dynamic planning I, BFS of queue (70.121.279.200)
理想之光不灭
Dimension table and fact table in data warehouse
基于 Nebula Graph 构建百亿关系知识图谱实践
Leetcode -- number of palindromes
Introduction to database system Chapter 1 short answer questions - how was the final exam?
Remove the underline in router link
Bone conduction non ear Bluetooth headset brand, bone conduction Bluetooth headset brand recommendation