12.springboot中常用的异常处理方式

Posted 结构化思维wz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了12.springboot中常用的异常处理方式相关的知识,希望对你有一定的参考价值。

springboot中常用的异常处理方式

在这里插入图片描述

springboot中既可以用spring中的@ControllerAdvice 进行统一处理,也可以采用springboot中的自定义异常处理方案,springboot中对异常处理有默认的方案,当然我们一般会自定义异常处理方案。

## 一般默认的错误页面:
因为开发者没有明确提供一个 /error 路径,如果开发者提供了 /error 路径 ,这个页面就不会展示出来,不过在 Spring Boot 中,提供 /error 路径实际上是下下策,Spring Boot 本身在处理异常时,也是当所有条件都不满足时,才会去找 /error 路径。那么我们就先来看看,在 Spring Boot 中,如何自定义 error 页面,整体上来说,可以分为两种,一种是静态页面,另一种是动态页面。

优先级:动态详细>静态详细>动态模糊>静态模糊

静态页面

  • 默认路径

    ---static---
    
    ​		---error---
    
    ​			---404.html----       只展示404错误
    
    ​			----4xx.html---- 	  展示400-499错误,文件中如果有404等更明确的,会按照更明确地
    

动态页面

使用页面模板来做,更为灵活。

默认路径也需要在error下!

----templates-----
	----error-----
		----500.html---
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <title>动态异常处理</title>
</head>
<body>
<table border="1">
  <tr>
    <td>path</td>
    <td th:text="${path}"></td>
  </tr>
  <tr>
    <td>error</td>
    <td th:text="${error}"></td>
  </tr>
  <tr>
    <td>message</td>
    <td th:text="${messages}"></td>
  </tr>
  <tr>
    <td>timestamp</td>
    <td th:text="${timestamp}"></td>
  </tr>
  <tr>
    <td>status</td>
    <td th:text="${status}"></td>
  </tr>
</table>

</body>
</html>
</body>
</html>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vRuado20-1622022281419)(D:\\OneDrive\\桌面\\springboot2\\springboot王泽\\动态异常页面.png)]

自定义异常(数据)

  • 在源码中我们可以看到 springboot 中关于异常的五条数据的定义:

    @Override
    public Map<String, Object> getErrorAttributes(ServerRequest request,
                    boolean includeStackTrace) {
            Map<String, Object> errorAttributes = new LinkedHashMap<>();
            errorAttributes.put("timestamp", new Date());
            errorAttributes.put("path", request.path());
            Throwable error = getError(request);
            HttpStatus errorStatus = determineHttpStatus(error);
            errorAttributes.put("status", errorStatus.value());
            errorAttributes.put("error", errorStatus.getReasonPhrase());
            errorAttributes.put("message", determineMessage(error));
            handleException(errorAttributes, determineException(error), includeStackTrace);
            return errorAttributes;
    }
    

    如果开发者没有自己提供一个 ErrorAttributes 的实例的话,那么 Spring Boot 将自动提供一个ErrorAttributes 的实例,也就是 DefaultErrorAttributes 。

  • 开发者自定义 ErrorAttributes 有两种方式 :

    1. 直接实现 ErrorAttributes 接口
    2. 继承 DefaultErrorAttributes(推荐),因为 DefaultErrorAttributes 中对异常数据的处理已经完成,开发者可以直接使用。
  • 实现方式:

    @Component
    public class MyErrorAttributes extends DefaultErrorAttributes {
        @Override
        public Map<String, Object> getErrorAttributes(WebRequest webRequest, ErrorAttributeOptions options) {
            Map<String, Object> map = super.getErrorAttributes(webRequest, options);
            if((Integer) map.get("status") == 404){
                map.put("message","没有此页面");
    
            }
            return map;
        }
    }
    

自定义异常(视图)不常用,因为springboot提供的已经很够了

以上是关于12.springboot中常用的异常处理方式的主要内容,如果未能解决你的问题,请参考以下文章

java项目中的异常处理

iOS中异常处理机制使用小结

异常处理与常用类

JAVA中处理异常的5个常用关键字是啥?代表啥含义?

解析Exception和C#处理Exception的常用方法总结

解析Exception和C#处理Exception的常用方法总结