SpringBoot 全局异常处理终极指南

在日常开发中,异常处理几乎是绕不过去的一个话题。尤其在 后端 API 项目 中,如果没有统一的异常处理机制,很容易出现以下问题:
• Controller 层代码里充斥着 try-catch,显得冗余。
@RestController
@RequestMapping("/user")
publicclassUserController {

    @GetMapping("/{id}")
    public ApiResponse<String> getUser(@PathVariableint id) {
        if (id == 0) {
            thrownewBusinessException(404, "用户不存在");
        }
        return ApiResponse.success("用户ID: " + id);
    }
}
• 前端拿到的错误响应格式不一致,增加解析成本。
• 系统异常与业务异常混杂,难以追踪和排查。
因此,在 Spring Boot 项目中,我们通常会通过 全局异常处理 来收敛所有错误,保证接口返回结构的统一性,并简化开发。
图片
01
为什么需要全局异常处理?
图片
在没有全局异常处理之前,开发者常常这样写代码:
@GetMapping("/{id}")
public User getUser(@PathVariableint id) {
    try {
        return userService.findById(id);
    } catch (Exception e) {
        e.printStackTrace();
        returnnull;
    }
}
问题在于:
  • • try-catch 逻辑冗余,重复代码太多。
  • • 一旦抛出异常,返回值不明确,前端无法准确感知错误信息。
👉 解决方案就是使用 Spring Boot 提供的全局异常处理机制:@ControllerAdvice + @ExceptionHandler
图片
02
定义统一返回结果
图片
首先定义一个通用的响应对象 ApiResponse,用于统一接口返回格式:
publicclassApiResponse<T> {
    privateint code;
    private String message;
    private T data;

    publicApiResponse(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    publicstatic <T> ApiResponse<T> success(T data) {
        returnnewApiResponse<>(200, "success", data);
    }

    publicstatic <T> ApiResponse<T> error(int code, String message) {
        returnnewApiResponse<>(code, message, null);
    }

    // getter & setter
}
这样一来,无论成功还是失败,都能保证返回结果的结构一致。
图片
03
自定义业务异常
图片
除了系统异常(NullPointerExceptionSQLException 等),我们还需要定义 业务异常,用于明确业务逻辑错误:
publicclassBusinessExceptionextendsRuntimeException {
    privateint code;

    publicBusinessException(int code, String message) {
        super(message);
        this.code = code;
    }

    publicintgetCode() {
        return code;
    }
}
例如:用户不存在、余额不足、参数非法等,都可以通过 BusinessException 来抛出。
图片
04
编写全局异常处理器
图片
接下来,通过 @RestControllerAdvice来统一捕获异常:
@RestControllerAdvice
publicclassGlobalExceptionHandler {

    // 处理业务异常
    @ExceptionHandler(BusinessException.class)
    public ApiResponse<?> handleBusinessException(BusinessException ex) {
        return ApiResponse.error(ex.getCode(), ex.getMessage());
    }

    // 处理参数校验异常
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ApiResponse<?> handleValidException(MethodArgumentNotValidException ex) {
        Stringmsg= ex.getBindingResult().getFieldError().getDefaultMessage();
        return ApiResponse.error(400, msg);
    }

    // 兜底异常处理
    @ExceptionHandler(Exception.class)
    public ApiResponse<?> handleException(Exception ex) {
        ex.printStackTrace(); // 可接入日志系统
        return ApiResponse.error(500, "服务器内部错误");
    }
}
这样,不管是业务异常还是系统异常,都会走到全局处理器,保证返回结果的统一性。
图片
05
使用示例
图片
Controller
请求与响应
• 正常请求:
{
  "code":200,
  "message":"success",
  "data":"用户ID: 1"
}
• 业务异常:
{
  "code":404,
  "message":"用户不存在",
  "data":null
}
• 系统异常:
{
  "code":500,
  "message":"服务器内部错误",
  "data":null
}
图片
06
总结
图片
通过 全局异常处理,我们实现了以下目标:
• 统一返回结构,方便前端解析。
• 集中管理异常,减少冗余 try-catch
• 区分业务异常与系统异常,提升代码可维护性。
• 可扩展性强,后续可以接入日志系统(如 LogbackELK)或异常监控平台(如 Sentry)。
建议在实际项目中,将 全局异常处理 作为基础框架的一部分,避免每个 Controller 重复造轮子。
图片
07
日志落库 / ELK 接入最佳实践
图片
在真实的生产环境中,仅仅返回统一的错误信息还不够。我们还需要对异常进行持久化存储与分析,以便后续排查问题和改进系统。
常见的做法有两种:
1. 日志落库(数据库存储)
在全局异常处理器中,可以将异常信息写入数据库(例如 MySQL、PostgreSQL):
@ExceptionHandler(Exception.class)
public ApiResponse<?> handleException(Exception ex, HttpServletRequest request) {
    // 记录日志信息
    ErrorLoglog=newErrorLog();
    log.setUri(request.getRequestURI());
    log.setMethod(request.getMethod());
    log.setMessage(ex.getMessage());
    log.setStackTrace(Arrays.toString(ex.getStackTrace()));
    log.setCreateTime(LocalDateTime.now());

    errorLogRepository.save(log); // JPA 或 MyBatis 保存

    return ApiResponse.error(500, "服务器内部错误");
}
其中 ErrorLog 可以定义为:
@Entity
publicclassErrorLog {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String uri;
    private String method;
    private String message;
    private String stackTrace;
    private LocalDateTime createTime;

    // getter & setter
}
这样就能将异常详细信息落到数据库,方便后续查询与统计。
2. 接入 ELK(Elasticsearch + Logstash + Kibana)
如果系统日志量较大,推荐接入 ELK,实现实时日志收集与可视化。
1)配置 Logback 输出 JSON 日志
logback-spring.xml 中使用 logstash-logback-encoder 输出 JSON:
<configuration>
    <appendername="LOGSTASH"class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>logs/app-log.json</file>
        <encoderclass="net.logstash.logback.encoder.LoggingEventEncoder"/>
    </appender>

    <rootlevel="INFO">
        <appender-refref="LOGSTASH"/>
    </root>
</configuration>
2)通过 Logstash 收集日志
配置 Logstash(logstash.conf):
input {
  file {
    path =>"/usr/local/app/logs/app-log.json"
    codec => json
  }
}

output {
  elasticsearch {
    hosts => ["http://localhost:9200"]
    index => "springboot-error-%{+YYYY.MM.dd}"
  }
}
3)在 Kibana 可视化
通过 Kibana Dashboard,可以实现:
• 错误趋势图
• 按 API 维度统计异常数量
• 按时间维度分析错误高峰
• 搜索具体异常堆栈
3. 最佳实践建议
• 开发环境: 控制台打印异常即可,方便调试。
• 测试环境: 日志落库,方便 QA 排查问题。
• 生产环境: 接入 ELK,实时收集和可视化,必要时配合 告警系统(如飞书/钉钉机器人、Prometheus Alertmanager)。
图片
08
飞书机器人告警接入
图片
在生产环境中,仅仅记录日志还不够。当发生严重异常时,我们希望能 实时收到告警通知,避免问题被埋没。常见的方式就是接入 企业 IM 工具(如飞书、钉钉、企业微信)。
下面以 飞书自定义机器人 为例,展示如何在全局异常处理器中推送告警。
1. 创建飞书自定义机器人
打开飞书群聊 → 设置 → 群机器人 → 添加机器人 → 选择 自定义机器人。
复制生成的 Webhook 地址,类似:
https://open.feishu.cn/open-apis/bot/v2/hook/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx
2. 编写工具类(推送异常消息)
使用 RestTemplate 发送 POST 请求:
@Component
publicclassFeishuBotNotifier {

    privatestaticfinalStringWEBHOOK_URL="https://open.feishu.cn/open-apis/bot/v2/hook/xxxxxx";

    privatefinalRestTemplaterestTemplate=newRestTemplate();

    publicvoidsendAlert(String title, String content) {
        Map<String, Object> body = newHashMap<>();
        body.put("msg_type", "text");
        Map<String, String> text = newHashMap<>();
        text.put("text", String.format("【异常告警】\n标题: %s\n详情: %s", title, content));
        body.put("content", text);

        restTemplate.postForEntity(WEBHOOK_URL, body, String.class);
    }
}
3. 在全局异常处理器中调用
当捕获到异常时,推送到飞书:
@RestControllerAdvice
@RequiredArgsConstructor
publicclassGlobalExceptionHandler {

    privatefinal FeishuBotNotifier feishuBotNotifier;

    @ExceptionHandler(Exception.class)
    public ApiResponse<?> handleException(Exception ex, HttpServletRequest request) {
        // 构造告警信息
        Stringtitle="SpringBoot 服务异常";
        Stringcontent= String.format("URI: %s\nMethod: %s\n错误: %s",
                request.getRequestURI(), request.getMethod(), ex.getMessage());

        // 发送飞书告警
        feishuBotNotifier.sendAlert(title, content);

        ex.printStackTrace();
        return ApiResponse.error(500, "服务器内部错误");
    }
}
4. 飞书群内效果
触发异常后,群内会收到类似消息:
【异常告警】
标题: SpringBoot 服务异常
详情: 
URI: /user/0
Method: GET
错误: 用户不存在
图片
09
总结(终极版 🚀)
图片
到这里,我们的全局异常管理方案已经形成了一整套闭环:
  • • 全局异常处理:统一返回格式,简化开发。
  • • 业务异常区分:明确业务错误与系统错误。
  • • 日志落库:异常可持久化追溯。
  • • ELK 接入:日志可视化分析,支持查询与报表。
  • • 飞书机器人告警:重大异常实时通知,提高运维响应速度。
这样一套机制,基本涵盖了 从接口开发 → 异常收敛 → 日志分析 → 实时告警 的完整链路,既保证了系统的可维护性,也提升了线上运维的响应效率。
来源:https://blog.csdn.net/qq_41688840
SpringBoot 全局异常处理终极指南
posted @ 2025-09-08 14:08  CharyGao  阅读(200)  评论(0)    收藏  举报