【笔记】SSM异常处理器

前言

SSM异常处理器学习笔记
异常要分类处理
异常要都抛到表现层处理
异常要利用AOP思想处理

出现异常的情况

  • 框架内部抛出的异常:因使用不合规导致
  • 数据层抛出的异常:因外部服务器故障导致(例如:服务器访问超时)
  • 业务层抛出的异常:因业务逻辑书写错误导致(例如:遍历业务书写操作,导致索引异常等)
  • 表现层抛出的异常:因数据收集、校验等规则导致(例如:不匹配的数据类型间导致异常)
  • 工具类抛出的异常:因工具类书写不严谨不够健壮导致(例如:必要释放的连接长期未释放等)

异常的分类

  • 业务异常(BusinessException)
    • 规范的用户行为产生的异常
    • 不规范的用户行为操作产生的异常
  • 系统异常(SystemException)
    • 项目运行过程中可预计且无法避免的异常
  • 其他异常(Exception)
    • 编程人员未预期到的异常

异常的解决方案

  • 业务异常(BusinessException)
    • 发送对应消息传递给用户,提醒规范操作
  • 系统异常(SystemException)
    • 发送固定消息传递给用户,安抚用户
    • 发送特定消息给运维人员,提醒维护
    • 记录日志
  • 其他异常(Exception)
    • 发送固定消息传递给用户,安抚用户
    • 发送特定消息给运维人员,提醒维护
    • 记录日志

封装ResponseResult类

传送门

封装Code类

src/main/java/com/pojo/ExceptionCode.java
1
2
3
4
5
6
7
package com.pojo;

public class ExceptionCode {
public static final Integer SYSTEM_ERROR = 50001;
public static final Integer BUSINESS_ERROR = 50002;
public static final Integer UNKNOWN_ERROR = 59999;
}

封装系统异常

src/main/java/com/exception/SystemException.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.exception;

public class SystemException extends RuntimeException {

private Integer 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;
}

public Integer getCode() {
return code;
}

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

封装业务异常

src/main/java/com/exception/BusinessException.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.exception;

public class BusinessException extends RuntimeException {

private Integer 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;
}

public Integer getCode() {
return code;
}

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

添加拦截异常的配置类

  • 方法返回的数据会直接返回给前端

@RestControllerAdvice:指定这是一个异常处理器
@ExceptionHandler(Exception.class):指定拦截的异常类型

src/main/java/com/exception/ProjectExceptionAdvice.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.exception;

import com.pojo.ExceptionCode;
import com.pojo.ResponseResult;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class ProjectExceptionAdvice {

@ExceptionHandler(SystemException.class)
public ResponseResult doSystemException(SystemException exception) {
// 记录日志
// 发送消息给运维
// 发送邮件给开发人员,将exception对象发送给开发人员
// 响应数据
return new ResponseResult(exception.getCode(), exception.getMessage());
}

@ExceptionHandler(BusinessException.class)
public ResponseResult doBusinessException(BusinessException exception) {
// 直接响应数据
return new ResponseResult(exception.getCode(), exception.getMessage());
}

@ExceptionHandler(Exception.class)
public ResponseResult doException(Exception exception) {
// 记录日志
// 发送消息给运维
// 发送邮件给开发人员,将exception对象发送给开发人员
// 响应数据
return new ResponseResult(ExceptionCode.UNKNOWN_ERROR, "系统繁忙");
}
}

在Spring配置类中添加包扫描

src/main/java/com/conf/SpringConfig.java
1
2
3
4
@Configuration
@ComponentScan({"com.exception"})
public class SpringConfig {
}

完成

参考文献

哔哩哔哩——黑马程序员