Springboot统一异常处理(详细配置)

本文介绍了Springboot如何实现全局异常处理,包括没有异常处理的情况、添加全局统一异常处理类和自定义异常处理。通过自定义异常类和处理方法,确保了系统能够优雅地处理各种异常情况。

关于异常,参见:
Java异常

没有异常处理

controller中测试类:

 @GetMapping("/testException")
    public Integer testException(Integer a,Integer b) {
        return a+b;
    }

测试:

如果参数a和b中有一个为空,则会报空指针异常:

浏览器页面长这样:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tMWuB6qr-1590583383167)(http://img.liuyj.top/统一异常处理-3.jpg)]

全局统一异常处理

添加全局统一异常处理类:

package com.example.conf;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * @author liuyj
 * @Title: ControllerHandler 自定义处理异常
 * @create 2020-05-14 10:45
 * @ProjectName test
 * @Description: TODO
 */
@ControllerAdvice
public class ControllerHandler {

    @ExceptionHandler
    @ResponseBody
    @ResponseStatus(HttpStatus.OK)
    public ResultInfo handleException(Exception ex){
        System.out.println("程序异常:"+ex.toString());
        return new ResultInfo(5000,"请求失败");
    }
}

再次测试:

自定义异常处理

添加异常UserNotExistException类:

public class UserNotExistException extends Exception{
    private Integer id;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public UserNotExistException(Integer id){
        this.id=id;
    }
    @Override
    public String toString() {
        return "用户不存在";
    }
}

controller抛出异常:

  @PostMapping("/getUser")
    public boolean getUser() throws UserNotExistException {
        throw new UserNotExistException(10);
    }

先测试,我们只是抛出异常,并没有对异常进行处理:

添加异常处理:

package com.example.conf;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * @author liuyj
 * @Title: ControllerHandler 自定义处理异常
 * @create 2020-05-14 10:45
 * @ProjectName test
 * @Description: TODO
 */
@ControllerAdvice
public class ControllerHandler {

    @ExceptionHandler
    @ResponseBody
    @ResponseStatus(HttpStatus.OK)
    public ResultInfo handleException(Exception ex){
        System.out.println("程序异常:"+ex.toString());
        return new ResultInfo(5000,"请求失败");
    }

    /**
     * 处理UserNotExistException异常
     */
    @ExceptionHandler({UserNotExistException.class})
    @ResponseBody
    @ResponseStatus(HttpStatus.OK)
    public ResultInfo handleUserNotExistException(UserNotExistException ex) {
        System.out.println("请求用户数据异常:" + ex.toString());
        return new ResultInfo(5000, "请求用户数据失败");
    }
}

测试:

可以看到已经处理。
测试之前的空指针异常,也没问题

被要求单独处理的异常会先被处理,而后其他异常会被Exception(默认形式)的处理方法捕获。

-------文末:
欢迎访问:
微信公众号(程序员资料站):code_data

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员资料站

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值