SpringBoot 统一异常处理 ControllerAdvice

转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/80678034
本文出自【赵彦军的博客】

在用spring Bootweb后台时,经常会出现异常,如果每个异常都自己去处理很麻烦,所以我们创建一个全局异常处理类来统一处理异常。通过使用@ControllerAdvice定义统一的异常处理类,而不是在每个Controller中逐个定义。

ControllerAdvice

@ControllerAdvice,是Spring3.2提供的新注解,从名字上可以看出大体意思是控制器增强。

实例讲解

下面我们通过一个例子,捕获 IndexOutOfBoundsException 异常,然后统一处理这个异常,并且给用户返回统一的响应。

创建异常统一处理类:ExceptionAdvice

package com.yiba.didiapi.exception;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class ExceptionAdvice {

    @ExceptionHandler({ IndexOutOfBoundsException.class })
    @ResponseBody
    public String handleIndexOutOfBoundsException(Exception e) {
        e.printStackTrace();
        return "testArrayIndexOutOfBoundsException";
    }

}

定义 ApiController

@RestController
public class ApiController {

    @GetMapping("getUser")
    public String getUser() {
        List<String> list = new ArrayList<>();
        return list.get(2);
    }

}

可以看到在 getUser 方法中, 会抛出 IndexOutOfBoundsException 异常。但是这个异常不会通过接口抛给用户,会被 ExceptionAdvice 类拦截,下面我们用 postMan 验证一下。

这里写图片描述

统一处理自定义异常

自定义 GirlException 异常

public class GirlException extends RuntimeException {

    private Integer code;

    public GirlException(Integer code, String msg) {
        super(msg);
        this.code = code;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

}

controller 里面抛出异常,新建ApiController

@RestController
public class ApiController {

    @GetMapping("getUser/{id}")
    public String getUser(@PathVariable("id") Integer id) {
        if (id == 0) {
            throw new GirlException(101, "年龄太小了");
        } else if (id == 1) {
            throw new GirlException(102, "年龄不够18岁");
        }
        return "ok";
    }
}

自定义统一返回对象 ResultUtil

public class ResultUtil {

    int code;
    String mes;

    public ResultUtil(int code, String mes) {
        this.code = code;
        this.mes = mes;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMes() {
        return mes;
    }

    public void setMes(String mes) {
        this.mes = mes;
    }
}

创建异常统一处理类:ExceptionAdvice

@ControllerAdvice
public class ExceptionAdvice {

    @ExceptionHandler({Exception.class})
    @ResponseBody
    public ResultUtil handleIndexOutOfBoundsException(Exception e) {
        ResultUtil resultUtil;
        if (e instanceof GirlException) {
            GirlException girlException = (GirlException) e;
            resultUtil = new ResultUtil(girlException.getCode(), girlException.getMessage());
            return resultUtil;
        }
        return new ResultUtil(0, "未知异常");
    }
}

测试

这里写图片描述


个人微信号:zhaoyanjun125 , 欢迎关注

猜你喜欢

转载自blog.csdn.net/zhaoyanjun6/article/details/80678034