使用@ControllerAdvice和@ExceptionHandler实现全局异常处理,通过定义ErrorResponse统一响应格式,结合自定义BusinessException抛出业务异常,实现空指针、业务及未捕获异常的拦截与规范返回,提升系统稳定性和前后端交互一致性。

在Java开发中,尤其是使用Spring或Spring Boot框架时,实现全局异常拦截机制可以统一处理程序中抛出的异常,避免重复的try-catch代码,同时提升接口返回的规范性和用户体验。核心方式是使用 @ControllerAdvice 和 @ExceptionHandler 注解来实现全局异常处理。
注意:@ControllerAdvice 是一个特殊的@Component,能够作用于所有 @Controller 标记的类,配合 @ExceptionHandler 可以捕获控制器中抛出的异常。
创建一个全局异常处理类:
@ControllerAdvice
public class GlobalExceptionHandler {
// 处理空指针异常
@ExceptionHandler(NullPointerException.class)
@ResponseBody
public ResponseEntity<String> handleNullPointer(NullPointerException e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body("发生空指针异常:" + e.getMessage());
}
// 处理自定义业务异常
@ExceptionHandler(BusinessException.class)
@ResponseBody
public ResponseEntity<ErrorResponse> handleBusinessException(BusinessException e) {
ErrorResponse error = new ErrorResponse(e.getCode(), e.getMessage());
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(error);
}
// 处理所有未被捕获的异常
@ExceptionHandler(Exception.class)
@ResponseBody
public ResponseEntity<ErrorResponse> handleGeneralException(Exception e) {
ErrorResponse error = new ErrorResponse("500", "系统内部错误");
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(error);
}
}
为了前后端交互更清晰,建议封装一个通用的错误响应体:
立即学习“Java免费学习笔记(深入)”;
public class ErrorResponse {
private String code;
private String message;
public ErrorResponse(String code, String message) {
this.code = code;
this.message = message;
}
// getter 和 setter 省略
}
便于在业务逻辑中主动抛出有意义的异常:
public class BusinessException extends RuntimeException {
private String code;
public BusinessException(String code, String message) {
super(message);
this.code = code;
}
// getter 方法
public String getCode() {
return code;
}
}
在服务中使用:
if (user == null) {
throw new BusinessException("USER_NOT_FOUND", "用户不存在");
}
以上就是Java中如何实现全局异常拦截机制的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号