ControllerAdvice+ExceptionHandler配置springboot全局异常

springboot全局异常配置

全局异常处理的说明及优势

将系统的异常放到统一处理,不用每个方法都去处理异常。统一系统的异常信息格式及简化开发,灵活配置异常信息用于对应不同的外部系统对异常码不同的需求。

依赖配置

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
</dependency>

具体实现

真实开发中需要记录异常日志信息,方便错误排除

package com.fiapi.forwardservice.util;

import com.fiapi.forwardservice.exception.CustomizeException;
import com.fiapi.forwardservice.exception.OpenInterfaceException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

/**
 * TODO
 * 全局异常处理类
 * @author Administrator
 * @version 1.0
 * @date 2021/7/30 11:08
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 自定义异常
     * @Author  pengdeyu
     * @Date   2021/7/30
     */
    @ExceptionHandler(value = CustomizeException.class)
    public Object handlerExcetion(CustomizeException ce){
        Map<String,String> map = new HashMap();
        map.put("code",ce.getErrorCode());
        map.put("msg",ce.getMessage());
        return map;
    }

    /**
     * 自定义异常
     * @Author  pengdeyu
     * @Date   2021/7/30
     */
    @ExceptionHandler(value = OpenInterfaceException.class)
    public Object handlerExcetion(OpenInterfaceException oe){
        Map<String,String> map = new HashMap();
        map.put("code","2000");
        map.put("msg",oe.getMessage());
        return map;
    }

    /**
     * 总的异常处理类  防止异常被漏掉
     * @Author  pengdeyu
     * @Date   2021/7/30
     */
    @ExceptionHandler(value = Exception.class)
    public Object handlerExcetion(Exception ex){
        Map<String,String> map = new HashMap();
        map.put("code","1000");
        map.put("msg",ex.getMessage());
        return map;
    }

}

--接受请求并抛出异常

package com.fiapi.forwardservice.forward;

import com.fiapi.forwardservice.exception.CustomizeException;
import com.fiapi.forwardservice.exception.OpenInterfaceException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Administrator
 * @version 1.0
 * @date 2021/7/30 11:21
 */
@RestController
@RequestMapping("exception/")
public class ExceptionController {

    @GetMapping("CeTest")
    public Object CeTest() throws CustomizeException {
        throw new CustomizeException("9999","自定义异常抛出");
    }

    @GetMapping("OeTest")
    public Object OeTest() throws OpenInterfaceException {
        throw new OpenInterfaceException("外部接口异常抛出");
    }

    @GetMapping("ExTest")
    public Object ExTest() throws Exception {
        throw new Exception("系统异常抛出");
    }

}


实现效果

全局异常测试
在这里插入图片描述
自定义异常测试
在这里插入图片描述
在这里插入图片描述

常见问题

全局配置不生效

异常配置不生效
1、@RestControllerAdvice,@ControllerAdvice注解是否添加
2、springboot启动时是否扫描到全局配置类

异常与预期不匹配

在抛出预期异常之前或之后触发了系统异常,会造成抛出的异常与预期不符
示例如下:

@GetMapping("CeTest")
public Object CeTest(@RequestParam String a) throws CustomizeException {
    try{
        // 1/0报错
        new BigDecimal(1).divide(new BigDecimal(0));
        return 111;
    }catch(Exception e){
        //预期抛出CustomizeException异常
        throw new CustomizeException("9999","自定义异常抛出");
    }finally {
        // 1/0报错
        new BigDecimal(1).divide(new BigDecimal(0));
    }
}

最终结果
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值