Skip to content

common-exception-starter

1. 功能介绍

1.统一异常处理

  • 通过 @RestControllerAdvice@ExceptionHandler 实现全局异常捕获,减少重复代码,提升异常处理效率。
  • 对常见异常(如参数校验失败、绑定失败、业务逻辑异常)进行精准捕获,并返回结构化的响应数据。

2.自定义异常支持

  • 提供 CustomException 类,支持自定义业务异常,通过状态码和异常信息精确描述错误原因。
  • 支持基于枚举类(如 RespBeanEnum)的异常封装,规范异常定义,提升代码可读性与可维护性。

3.安全与用户友好

  • 捕获所有未处理的系统异常,避免系统直接暴露内部信息,提升安全性。
  • 返回用户友好的错误提示,增强用户体验。

2.案例演示

1.创建模块

CleanShot 2025-01-20 at 13.29.57@2x

2.目录结构

CleanShot 2025-01-20 at 13.41.43@2x

3.pom.xml

xml
    <dependencies>
        <!-- common-exception-starter -->
        <dependency>
            <groupId>cn.sunxiansheng</groupId>
            <artifactId>common-exception-starter</artifactId>
        </dependency>
        <!-- 必须引入 -->
        <dependency>
            <groupId>cn.sunxiansheng</groupId>
            <artifactId>common-log4j2-starter</artifactId>
        </dependency>

        <!-- 以下依赖用于测试 -->
        <!-- common-validation-starter -->
        <dependency>
            <groupId>cn.sunxiansheng</groupId>
            <artifactId>common-validation-starter</artifactId>
        </dependency>
        <!-- 测试用的Web模块 -->
        <dependency>
            <groupId>cn.sunxiansheng</groupId>
            <artifactId>common-web-starter</artifactId>
        </dependency>
    </dependencies>

4.application.yml 配置日志存储根目录

yaml
sun-rays:
  log4j2:
    home: /Users/sunxiansheng/IdeaProjects/sunrays-framework-demo/common-exception-starter-demo/logs # 日志根目录(默认./logs)

5.ExceptionController.java 测试三种异常处理

java
package cn.sunxiansheng.exception.controller;

import cn.sunxiansheng.exception.CustomException;
import lombok.Data;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * Description: 测试异常处理
 *
 * @Author sun
 * @Create 2024/10/28 17:41
 * @Version 1.0
 */
@RestController
public class ExceptionController {

    @Data
    static class User {
        @NotBlank(message = "用户名不能为空")
        private String name;
        @NotNull(message = "年龄不能为空")
        private Integer age;
    }

    /**
     * 测试异常
     *
     * @return
     */
    @RequestMapping("/testException")
    public String testException() {
        int i = 1 / 0;
        return "testException";
    }

    /**
     * 测试参数校验异常
     *
     * @param user
     * @return
     */
    @RequestMapping("/testMethodArgumentNotValidException")
    public String testMethodArgumentNotValidException(@RequestBody @Valid User user) {
        int i = 1 / 0;
        return "testException";
    }

    /**
     * 测试自定义异常
     *
     * @return
     */
    @RequestMapping("/testCustomException")
    public String testCustomException() {
        throw new CustomException(111, "自定义异常");
    }
}

6.ExceptionApplication.java 启动类

java
package cn.sunxiansheng.exception;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Description: ExceptionApplication
 *
 * @Author sun
 * @Create 2025/1/20 13:30
 * @Version 1.0
 */
@SpringBootApplication
public class ExceptionApplication {

    public static void main(String[] args) {
        SpringApplication.run(ExceptionApplication.class, args);
    }
}

7.启动测试

1.测试普通的异常

CleanShot 2025-01-20 at 13.45.53@2x

2.测试参数校验异常

CleanShot 2025-01-20 at 13.47.24@2x

3.测试自定义异常

CleanShot 2025-01-20 at 13.48.19@2x