后端异常处理:全局异常处理器

@RestControllerAdvice=@ControllerAdvice+@ResponseBody

@ExceptionHandler(Exception.class)

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.RestController;

@ControllerAdvice
@RestController
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public ResponseEntity<String> handleException(Exception e) {
        // 这里可以根据实际需求定制异常处理逻辑
        return ResponseEntity.status(500).body("发生了未知错误: " + e.getMessage());
    }

    @ExceptionHandler(YourCustomException.class)
    public ResponseEntity<String> handleYourCustomException(YourCustomException e) {
        // 这里可以根据实际需求定制自定义异常处理逻辑
        return ResponseEntity.status(400).body("发生了自定义异常: " + e.getMessage());
    }

    // 可以添加更多的异常处理方法来处理不同类型的异常

}

如果有Result标准返回类可以return Result.error("对不起,您的操作有问题");

相关推荐

  1. 异常处理:全局异常处理器

    2024-03-21 13:24:01       48 阅读
  2. SpringBoot 统一返回格式、处理全局异常

    2024-03-21 13:24:01       41 阅读
  3. WPF 全局异常处理

    2024-03-21 13:24:01       60 阅读
  4. Spring全局异常处理

    2024-03-21 13:24:01       60 阅读
  5. flask实践02-全局Response返回和异常处理

    2024-03-21 13:24:01       35 阅读
  6. SpringBoot中全局异常处理

    2024-03-21 13:24:01       44 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-21 13:24:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-21 13:24:01       101 阅读
  3. 在Django里面运行非项目文件

    2024-03-21 13:24:01       82 阅读
  4. Python语言-面向对象

    2024-03-21 13:24:01       91 阅读

热门阅读

  1. 亚信安慧AntDB全景观察:数据库领域的创新者

    2024-03-21 13:24:01       41 阅读
  2. FPGA_AD9361

    2024-03-21 13:24:01       43 阅读
  3. 力扣126双周赛

    2024-03-21 13:24:01       45 阅读
  4. electron-builder打包

    2024-03-21 13:24:01       46 阅读
  5. 物理设计概念 -- 物理层次结构

    2024-03-21 13:24:01       34 阅读
  6. [Open3d]: 知识记录

    2024-03-21 13:24:01       40 阅读
  7. mybatis---->tx中weekend类

    2024-03-21 13:24:01       43 阅读
  8. Mac传文件到云服务器

    2024-03-21 13:24:01       45 阅读
  9. 二叉树遍历144、94、145

    2024-03-21 13:24:01       41 阅读
  10. Python基础算法解析:决策树

    2024-03-21 13:24:01       37 阅读
  11. css第一个元素first-child匹配失败原因

    2024-03-21 13:24:01       38 阅读
  12. 后端使用前端页面的很好的推荐Layui

    2024-03-21 13:24:01       38 阅读