一、什么是统一异常处理

# 一、什么是统一异常处理

我们想让异常结果也显示为统一的返回结果对象,并且统一处理系统的异常信息,那么需要统一异常处理

# 二、统一异常处理

# 1、创建统一异常处理器

com.xxl.common.handler包中,创建统一异常处理类GlobalExceptionHandler.java

package com.xxl.common.handler;

/**
 * 统一异常处理类
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public R error(Exception e){
        e.printStackTrace();
        return R.error();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# 2、扫描异常处理器

确认启动类添加了注解@ComponentScan

/**
 * 启动类
 */
@SpringBootApplication
@ComponentScan(basePackages={"com.xxl.common"})
public class SpringbootInitApplication {
    
	public static void main(String[] args) {
        SpringApplication.run(SpringbootInitApplication.class, args);
    }
}
1
2
3
4
5
6
7
8
9
10
11

# 3、测试

增加异常方法

@RequestMapping("/testException")
@ResponseBody
public R testException() {
    Integer num = 1 / 0;
    return R.ok().data("num", num);
}
1
2
3
4
5
6

返回统一错误结果

{
    "success": false,
    "code": 20001,
    "message": "未知错误",
    "data":{}
}
1
2
3
4
5
6

# 三、处理特定异常

# 1、添加异常处理方法

GlobalExceptionHandler.java中添加

@ExceptionHandler(BadSqlGrammarException.class)
@ResponseBody
public R error(BadSqlGrammarException e){
    e.printStackTrace();
    return R.setResult(ResultCodeEnum.BAD_SQL_GRAMMAR);
}
1
2
3
4
5
6

# 3、恢复制造的异常

@TableField(value = "is_deleted")
private Boolean deleted;
1
2

# 四、另一个例子

# 1、制造异常

在swagger中测试新增讲师方法,输入非法的json参数,得到

HttpMessageNotReadableException
1

# 2、添加异常处理方法

GlobalExceptionHandler.java中添加

@ExceptionHandler(HttpMessageNotReadableException.class)
@ResponseBody
public R error(JsonParseException e){
    e.printStackTrace();
    return R.setResult(ResultCodeEnum.JSON_PARSE_ERROR);
}
1
2
3
4
5
6

# 五、自定义异常

# 1、创建自定义异常类

创建com.xxl.common.exception包,

创建MyException.java通用异常类 继承RuntimeException

RuntimeException对代码没有侵入性

package com.xxl.common.exception;

/**
 * 自定义异常
 *
 * @author xxl
 * @date 2025/1/5 18:04
 */
@Data
@ApiModel(value = "全局异常")
public class MyException extends RuntimeException {
    
    /**
     * 状态码
     */
    private Integer code;

    /**
     * 接受状态码和消息
     *
     * @param code    状态码
     * @param message 消息
     */
    public MyException(Integer code, String message) {
        super(message);
        this.code = code;
    }

    /**
     * 接收枚举类型
     *
     * @param resultCodeEnum 枚举类型
     */
    public MyException(ResultCodeEnum resultCodeEnum) {
        super(resultCodeEnum.getMessage());
        this.code = resultCodeEnum.getCode();
    }
}
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
35
36
37
38

# 2、业务中需要的位置抛出GuliException

讲师controller中分页查询方法中判断参数是否合法

public R pageQuery(......){
    if(page <= 0 || limit <= 0){
        //throw new GuliException(21003, "参数不正确1");
        throw new GuliException(ResultCodeEnum.PARAM_ERROR);
    }
    ......
}
1
2
3
4
5
6
7

# 3、添加异常处理方法

GlobalExceptionHandler.java中添加

@ExceptionHandler(GuliException.class)
@ResponseBody
public R error(GuliException e){
    e.printStackTrace();
    return R.error().message(e.getMessage()).code(e.getCode());
}
1
2
3
4
5
6
最近修改于: 2025/1/6 01:13:07
和宇宙温柔的关联
房东的猫