作者:冷冷gg 链接: https:// juejin.im/post/5ecf06bb f265da76bd1ac76a
为什么需要全局异常处理
在传统 Spring Boot 应用中, 我们 @ControllerAdvice 来处理全局的异常,进行统一包装返回
// 摘至 spring cloud alibaba console 模块处理
@ControllerAdvice
public class ConsoleExceptionHandler {
@ExceptionHandler(AccessException.class)
private ResponseEntity<String> handleAccessException(AccessException e) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(e.getErrMsg());
}
}复制代码
例如: ③ 处应用调用数据库异常,通过 @ControllerAdvice 包装异常请求响应给客户端