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