Spring Boot异常处理!!!

SpringBoot默认的处理异常的机制:SpringBoot 默认的已经提供了一套处理异常的机制。一旦程序中出现了异常 SpringBoot 会向/error 的 url 发送请求。在 springBoot 中提供了一个叫 BasicErrorController 来处理/error 请求,然后跳转到默认显示异常的页面来展示异常信息

如 果我 们 需 要 将 所 有 的 异 常 同 一 跳 转 到 自 定 义 的 错 误 页 面 , 需 要 再src/main/resources/

templates 目录下创建 error.html 页面。注意:名称必须叫 error

  1.controller:

/*
 * Copyright (c) 2020, 2024,  All rights reserved.
 *
 */
package com.by.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * <p>Project: springboot - DomeContorller</p>
 * <p>Powered by scl On 2024-01-15 10:02:34</p>
 * <p>描述:<p>
 *
 * @author 孙臣龙 [1846080280@qq.com]
 * @version 1.0
 * @since 17
 */
@Controller
public class DomeController {

    @RequestMapping("show1")
    public String show1() {
        System.out.println(6 / 0);
        return "error";
    }

    @RequestMapping("show2")
    public String show2() {
        String str=null;
        str.length();
        return "error";
    }
}

2.定义错误页面

error.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>error1</h1>
出错啦!!!!汗流浃背了!!!
<span th:text="${msg}"></span>
</body>
</html>

error.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>error2</h1>
出错啦!!!!汗流浃背了!!!
<span th:text="${msg}"></span>
</body>
</html>

3.定义全局异常处理类:

GlobalException:(普通的处理请求)

/*
 * Copyright (c) 2020, 2024,  All rights reserved.
 *
 */
package com.by.exception;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

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

/**
 * <p>Project: springboot - GlobalException</p>
 * <p>Powered by scl On 2024-01-15 10:11:29</p>
 * <p>描述:<p>
 *
 * @author 孙臣龙 [1846080280@qq.com]
 * @version 1.0
 * @since 17
 */
@Component
public class GlobalException implements HandlerExceptionResolver {

    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
        ModelAndView modelAndView = new ModelAndView();
        if (e instanceof NullPointerException){
            modelAndView.setViewName("error1");
        } else if (e instanceof ArithmeticException) {
            modelAndView.setViewName("error2");
        }
        modelAndView.addObject("msg",e.toString());
        return modelAndView;
    }
}

AjaxGlobalException:(ajax的请求方式,返回的使json数据)

/*
 * Copyright (c) 2020, 2024,  All rights reserved.
 *
 */
package com.by.exception;

import javafx.beans.property.adapter.ReadOnlyJavaBeanBooleanProperty;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

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

/**
 * <p>Project: springboot - AjaxGlobalException</p>
 * <p>Powered by scl On 2024-01-15 10:48:17</p>
 * <p>描述:<p>
 *
 * @author 孙臣龙 [1846080280@qq.com]
 * @version 1.0
 * @since 17
 */
//@ControllerAdvice
public class AjaxGlobalException {

    @ResponseBody
    @ExceptionHandler
    public Map errorHandler(Exception e){

        Map<String , Object> m = new HashMap<>();
        m.put("status",500);
        m.put("msg",e.toString());

        return m;
    }
}

相关推荐

  1. SpringBoot中全局异常处理

    2024-01-16 13:22:02       30 阅读
  2. Springboot之全局异常处理

    2024-01-16 13:22:02       33 阅读
  3. SpringBoot全局异常处理方法

    2024-01-16 13:22:02       23 阅读
  4. springboot全局处理sql异常

    2024-01-16 13:22:02       8 阅读
  5. springboot全局异常处理和自定义异常处理

    2024-01-16 13:22:02       45 阅读

最近更新

  1. WPF自定义模板--Lable

    2024-01-16 13:22:02       0 阅读
  2. 自动化发布:Conda包依赖的持续集成之旅

    2024-01-16 13:22:02       0 阅读
  3. 探索Conda世界:使用conda list命令的全面指南

    2024-01-16 13:22:02       0 阅读
  4. Spark SQL----内置函数Aggregate Functions

    2024-01-16 13:22:02       0 阅读
  5. 掌握Conda配置术:conda config命令的深度指南

    2024-01-16 13:22:02       0 阅读
  6. 常见加密算法介绍

    2024-01-16 13:22:02       1 阅读
  7. Unity3D批量修改名称工具

    2024-01-16 13:22:02       1 阅读
  8. Istio在微服务中释放服务网格的力量

    2024-01-16 13:22:02       1 阅读

热门阅读

  1. 基于Asterisk和TTS/ASR语音识别的配置示例

    2024-01-16 13:22:02       38 阅读
  2. 1-1.this指针&闭包&作用域

    2024-01-16 13:22:02       39 阅读
  3. 音视频中的DTS和PTS区别

    2024-01-16 13:22:02       47 阅读
  4. 解密SHFileOperation

    2024-01-16 13:22:02       34 阅读
  5. 第十四讲_css媒体查询

    2024-01-16 13:22:02       41 阅读
  6. golang切片(slice)详解

    2024-01-16 13:22:02       28 阅读