@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class)
@ResponseBody
public ResponseEntity<String> handleException(Exception ex) {
// 记录日志
log.error("系统异常:", ex);
// 返回友好的错误信息
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("系统繁忙,请稍后再试");
}
}
@ControllerAdvice :全局异常处理类注解
@ExceptionHandler:指定异常拦截