当前位置: 首页 > news >正文

Spring 统一异常处理怎么做?

1. 创建自定义异常类

自定义异常类可以帮助我们更好地描述异常的类型和原因。例如,定义一个通用的 ApiException 类:

package com.example.exception;public class ApiException extends RuntimeException {private final int code; // 自定义异常代码private final String message; // 异常信息public ApiException(int code, String message) {super(message);this.code = code;this.message = message;}public int getCode() {return code;}public String getMessage() {return message;}
}

2. 创建全局异常处理器

使用 @ControllerAdvice 注解定义一个全局异常处理器类,并通过 @ExceptionHandler 注解来捕获并处理特定类型的异常。

package com.example.advice;import com.example.exception.ApiException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;@ControllerAdvice
public class GlobalExceptionHandler {// 处理自定义异常@ExceptionHandler(ApiException.class)public ResponseEntity<ErrorResponse> handleApiException(ApiException ex) {ErrorResponse errorResponse = new ErrorResponse(ex.getCode(), ex.getMessage());return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);}// 处理其他未捕获的异常@ExceptionHandler(Exception.class)public ResponseEntity<ErrorResponse> handleException(Exception ex) {ErrorResponse errorResponse = new ErrorResponse(500, "Internal Server Error: " + ex.getMessage());return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);}
}

在上面的代码中,ErrorResponse 是一个简单的数据类,用于封装错误信息:

package com.example.advice;public class ErrorResponse {private final int code;private final String message;public ErrorResponse(int code, String message) {this.code = code;this.message = message;}public int getCode() {return code;}public String getMessage() {return message;}
}

3. 测试异常处理

创建一个控制器类,用于测试异常处理逻辑。

package com.example.controller;import com.example.exception.ApiException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class TestController {@GetMapping("/test-api-exception")public String testApiException() {throw new ApiException(400, "This is a custom API exception");}@GetMapping("/test-general-exception")public String testGeneralException() {throw new RuntimeException("This is a general exception");}
}

4. 启动类

创建一个 Spring Boot 启动类,启动应用。

package com.example;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class ExceptionHandlingApplication {public static void main(String[] args) {SpringApplication.run(ExceptionHandlingApplication.class, args);}
}

5. 测试结果

启动应用后,访问以下两个接口,观察异常处理的效果:

  • 访问 /test-api-exception

    • 返回的 JSON 响应:

      {"code": 400,"message": "This is a custom API exception"
      }
    • HTTP 状态码:400

  • 访问 /test-general-exception

    • 返回的 JSON 响应:

      {"code": 500,"message": "Internal Server Error: This is a general exception"
      }
    • HTTP 状态码:500

6. 说明

  • @ControllerAdvice:用于定义全局异常处理器类,可以捕获整个应用中的异常。

  • @ExceptionHandler:用于指定处理特定类型的异常的方法。

  • ResponseEntity:用于封装响应体和 HTTP 状态码。

  • HttpStatus:用于指定 HTTP 状态码。

通过上述实现,你可以集中处理项目中的异常,避免在每个控制器中重复编写异常处理代码,同时提供统一的错误响应格式,便于前端开发和调试。

http://www.xdnf.cn/news/787339.html

相关文章:

  • QT 5.9.2+VTK8.0实现等高线绘制
  • Ubuntu中SSH服务器安装使用
  • VMWare安装常见问题
  • MATLAB仿真生成无线通信网络拓扑推理数据集
  • HTTP连接管理——短连接,长连接,HTTP 流水线
  • 【notepad++】如何设置notepad++背景颜色?
  • 在Oxygen编辑器中使用DeepSeek
  • typescript的Interface和Type
  • DPO 算法
  • Gradle依赖管理全面指南:从基础到高级实践
  • Spring Boot整合Druid与Dynamic-Datasource多数据源配置:从错误到完美解决
  • 力扣HOT100之多维动态规划:1143. 最长公共子序列
  • ArrayList 类
  • Generate Permutation
  • 编译器对齐机制与硬件浮点计算详解
  • 春雪食品×MTC AI助手:创新驱动再升级,效率革命正当时!
  • PV操作的C++代码示例讲解
  • .Net Framework 4/C# 初识 C#
  • LeetCode 300 最长递增子序列
  • 电工基础【5】简单的电路设计接线实操
  • SpringCloud——Nacos注册中心、OpenFeign
  • 前端验证下跨域问题(npm验证)
  • DeepSeek 赋能 NFT:数字艺术创作与交易的革新密码
  • 数据库完整性
  • 18.04 update 报错:(appstreamcli:2822): GLib-ERROR
  • 《Effective Python》第六章 推导式和生成器——使用类替代生成器的 `throw` 方法管理迭代状态转换
  • 提升系统稳定性和可靠性的特殊线程(看门狗线程)
  • Electron桌面应用下,在拍照、展示pdf等模块时,容易导致应用白屏
  • DiskGenius专业版v6.0.1.1645:分区管理、数据恢复、备份还原,一应俱全!
  • PHP+mysql 美容美发预约小程序源码 支持DIY装修+完整图文搭建教程