springboot全局异常管理

介绍

本文将介绍springboot全局异常处理方式,包括系统异常和自定义业务异常

1、编写异常测试方法

这里定义了两个方法,分别为sysExceptionTest()bizExceptionTest()

package com.example.demo.controller;

import com.example.demo.exception.biz.BizException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestExceptionController {

    /**
     * 系统异常测试方法
     * @return
     */
    @RequestMapping("/sysException")
    public String sysExceptionTest(){
        // 模拟一个空指针异常
        String str = null;
        System.out.println(str.hashCode());
        return "index";
    }

    /**
     * 业务异常测试方法
     * @return
     */
    @RequestMapping("/bizException")
    public String bizExceptionTest(){
        // 自定义异常提示
        if(true){
            throw new BizException("-1","这是一个自定义异常提示");
        }
        return "index";
    }

}

2、自定义业务异常

package com.example.demo.exception;

public class BizException extends RuntimeException {

    private static final long serialVersionUID = 1L;

    protected String errorCode; //错误码
    protected String errorMsg; //错误信息

    public BizException() {
        super();
    }

    public BizException(String errorMsg) {
        super(errorMsg);
        this.errorMsg = errorMsg;
    }

    public BizException(String errorCode, String errorMsg) {
        super(errorCode);
        this.errorCode = errorCode;
        this.errorMsg = errorMsg;
    }

    public BizException(String errorCode, String errorMsg, Throwable cause) {
        super(errorCode, cause);
        this.errorCode = errorCode;
        this.errorMsg = errorMsg;
    }

    public String getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    public String getErrorMsg() {
        return errorMsg;
    }

    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }

    public String getMessage() {
        return errorMsg;
    }

    @Override
    public Throwable fillInStackTrace() {
        return this;
    }
}

3、全局异常捕获类

这里需要注意一点,如果是想将所有异常抛到统一的error界面,则instanceof Exception即可。如果是想将不同异常分别跳转至不同错误页,则需根据具体异常类型判断。

package com.example.demo.exception;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 全局异常类
 */
@Configuration
public class GlobalException implements HandlerExceptionResolver {

    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {

        ModelAndView mv = new ModelAndView();

        //1、所有异常统一抛到error.html界面
        if(e instanceof Exception){
            mv.setViewName("error");
        }

        //2、不同异常类型,跳转至自定义的不同界面
//        if(e instanceof NullPointerException){
//            mv.setViewName("error1");
//        }
//        if(e instanceof ArithmeticException){
//            mv.setViewName("error2");
//        }
//        if(e instanceof ClassCastException){
//            mv.setViewName("error3");
//        }
//        if(e instanceof ArrayIndexOutOfBoundsException){
//            mv.setViewName("error4");
//        }

        //3、将异常信息放入ModelAndView对象返回
        mv.addObject("errorMsg", e.toString());
        return mv;
    }
}

4、编写异常信息html模板

在templates目录下创建error.html模板,errorMsg为GlobalException中捕获返回的异常信息

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>异常信息界面</title>
</head>
<body>
        哎呀,服务器出错了!!!<br/>
        异常信息:<span th:text="${errorMsg}"></span>
</body>
</html>

5、效果查看

1、访问http://localhost:8080/sysException
在这里插入图片描述
2、访问http://localhost:8080/bizException
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值