SpringBoot统一异常处理

2023-05-16

概述

​ SpringBoot 提供了 @ControllerAdvice 、@RestControllerAdvice 注解可以实现统一异常处理,只需要在定义异常类加上以上注解即可。

自定义异常处理

定义统一异常处理

@RestControllerAdvice
public class GlobalExceptionHandler {

    private Logger logger = LoggerFactory.getLogger(getClass());

    /**
     *  拦截Exception类的异常
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Map<String,Object> exceptionHandler(Exception e){
        logger.error("系统异常Exception===",e);
        Map<String,Object> result = new HashMap<String,Object>();
        result.put("respCode", "9999");
        result.put("respMsg", "怎么又出错了");
        return result;
    }

    /**
     * 拦截 ServiceException 的异常
     * @param ex
     * @return
     */
    @ExceptionHandler(ServiceException.class)
    @ResponseBody
    public Map<String,Object> exceptionHandler(ServiceException ex){
        logger.info("自定义异常ServiceException:{}({})",ex.getMsg(), ex.getCode());
        Map<String,Object> result = new HashMap<String,Object>();
        result.put("respCode", ex.getCode());
        result.put("respMsg", ex.getMsg());
        return result;
    }

}

创建 controller 类

@RestController
@RequestMapping("/ex")
public class ExceptionController {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @RequestMapping("/testEx")
    public String testEx() {
        logger.debug("testEx>>>>>>");
        throw new ServiceException("501","自定义ServiceException异常");
    }

    @RequestMapping("/testEx1")
    public String testEx1() {
        logger.debug("testEx1>>>>>>");
        int a = 1/0;
        return "sucesss";
    }

    @RequestMapping("/test")
    public String test() {
        logger.debug("test>>>>>>");
        return "sucesss";
    }
}

测试异常

http://127.0.0.1:8080/ex/test 正常访问

http://127.0.0.1:8080/ex/testEx 自定义异常

http://127.0.0.1:8080/ex/testEx1 系统异常

tomcat further occurrences of HTTP header parsing errors will be logged at DEBUG level.

原访问地址: http://localhost:8080/ex/test

改为:http://127.0.0.1:8080/ex/test

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

SpringBoot统一异常处理 的相关文章

随机推荐

  • C语言初阶小练习(7)——指针(1)

    以下所有题都是用指针实现的 xff0c 你可能会想 xff0c 这些题目不用指针也可以写出来 xff0c 干嘛要多次一举呢 xff1f 非也 xff0c 这些只是初级指针 xff0c 先把基础打好 xff0c 后来指针的用途可大着呢 xff
  • C语言初阶小练习(8)——指针(2)

    还是一些指针小练习 xff0c 接上次 C语言小练习 xff08 7 xff09 指针 xff08 1 xff09 一起来看看吧 目录 编写程序输入n个整数 xff0c 查找并删除重复的数字 xff0c 打印结果 查找其中出现了多少个连续数
  • 推荐几个代码自动生成器,神器!!!

    20个代码生成框架 老的代码生成器的地址 xff1a https www cnblogs com skyme archive 2011 12 22 2297592 html 以下是大家推荐的最近很火爆的代码生成器神器 如果有更好的希望大家多
  • linux 安装discuz出现“ mysqli_connect()不支持advice_mysqli_connect ”解决方法

    由于不了解php相关技术 xff0c 所以在安装discuz的时候遇到了很多麻烦 xff0c 记录下 首先 xff0c 我的环境是CentOS6 5 xff0c 在安装discuz的时候需要yum很多东西 yum install php p
  • Ubuntu 22.04 LTS下Miniconda安装+换源(踩坑向)

    1 安装Miniconda 我使用的是Python3 8 xff0c 如果需要去其他对应版本 xff0c 请查看 Miniconda conda documentation 下载 wget https repo anaconda com m
  • FreeBSD修改为国内源

    禁用原来的FreeBSD conf ee etc pkg FreeBSD conf 将 enabled yes 改为 enabled no 保存 ESC 然后 a gt a 即可 创建另外一个 FreeBSD conf mkdir p us
  • 关于51单片机的中断

    1 中断的要求 1 中断源有中断请求 Ask for instructions of the CPU interrupt request source called interrupt source 2 此中断源的中断允许位为1 The i
  • 华为机试_HJ5 进制转换【简单】

    描述 写出一个程序 xff0c 接受一个十六进制的数 xff0c 输出该数值的十进制表示 数据范围 xff1a 保证结果在 1 le n le 2 31 1 1 n 231 1 输入描述 xff1a 输入一个十六进制的数值字符串 输出描述
  • bootstrap实现 — 个人简介

    实现 xff1a bootstrap 效果图 xff1a 源码 xff1a lt DOCTYPE html gt lt html gt lt head gt lt meta charset 61 34 utf 8 34 gt lt titl
  • 计蒜客--T1079--打表+控制输出

    假设有 N 盏灯 xff08 NN为不大于 5000 的正整数 xff09 xff0c 从 1 到 N 按顺序依次编号 xff0c 初始时全部处于开启状态 xff1b 有 M 个人 xff08 M 为不大于 N的正整数 xff09 也从 1
  • Authentication plugin ‘caching_sha2_password‘ 服务端也无法连接问题彻底解决

    在网上搜索了很多的帖子 xff0c 发现描述的都是外部客户端无法登录到mysql上 xff0c 登录上服务器以后连接更改配置的方式 xff0c 但是 xff01 xff01 xff01 xff01 xff01 我现在是服务器连接也报错啊啊啊
  • Hexo分类及标签显示

    Hexo根目录配置 config yml category map Blogs categories Blogs Tech categories Tech Tools categories Tools Other categories Ot
  • IDEA查看历史记录

    方法一 文件内 Ctrl 43 右键 Local History Show History xff0c 显示当前文件的本地修改历史 方法二 一 xff1a 在文件内 xff0c 按 Ctrl 43 Shift 43 A 弹出全部搜索对话框
  • SpringBoot-JPA整合ShardingShpere自定义分布式主键

    分布式主键简介 在分布式环境下 xff0c 由于分库分表导致数据水平拆分后无法使用单表自增主键 xff0c 因此我们需要一种全局唯一id生成策略作为分布式主键 当前有如下解决方案 UUID xff08 Universally Unique
  • Gitlab的安装与配置

    安装开始时 xff0c 需确认服务器最小配置是2核4G xff0c 因为gitlab软件比较大 1 配置yum源 xff1a vim etc yum repos d gitlab repo gitlab name 61 gitlab ce
  • Error creating bean with name ‘org.springframework.aop.aspectj.AspectJPointcutAdvisor#0

    问题 xff1a nested exception is org springframework beans factory BeanCreationException Error creating bean with name 39 or
  • Vue前端项目开发页面(二)

    前端界面开发 开发工具版本 64 vue cli 4 5 13 新建Login vue登陆页 1 在 vue exemples 项目 xff0c 选中components目录右键 New Vue Component xff0c 名称为 Lo
  • SpringBoot整合WebSocket

    概述 HTTP 协议是一种无状态的 无连接的 单向的应用层协议 它采用了请求 响应模型 通信请求只能由客户端发起 xff0c 服务端对请求做出应答处理 WebSocket和HTTP一样 xff0c 都是一种网络通信协议 比起HTTP只能由客
  • SpringBoot整合MybatisPlus使用IPage实现分页

    概述 MybatisPlus 提供了分页的功能 IPage内部原理是基于拦截器 xff0c 但是这个拦截的是方法以及方法中的参数 xff0c 这个也会判断是否是查询操作 如果是查询操作 xff0c 才会进入分页的处理逻辑 进入分页逻辑处理后
  • SpringBoot统一异常处理

    概述 SpringBoot 提供了 64 ControllerAdvice 64 RestControllerAdvice 注解可以实现统一异常处理 xff0c 只需要在定义异常类加上以上注解即可 自定义异常处理 定义统一异常处理 span