SpringBoot全局异常处理(三十)上

简介: SpringBoot全局异常处理(三十)上

一. 为什么要实现异常信息自定义展示

在 Springboot 项目开发中,包括以前的 SSM 框架开发中,我们常常会碰到 404 500 相应的错误信息.


如访问一个除以 0 的功能 (500) 错误

@RequestMapping("/div")
    @ResponseBody
    public String div(){
        int result=10/0;
        return "相除后的结果是:"+result;
    }


image.png

image.png


或者访问一个不存在的页面 404 错误

image.png

这两个页面都是 SpringBoot 默认提供的.


用户在使用的过程中,如果没有相应的开发或者网络经验,对这些提示信息是很讨厌的。


所以常常进行一些有趣的设计,来减轻项目运行错误导致的不良感受。


如: (以下图片来源于网络)


image.png

image.png



我们可以自定义错误页面.


二. 自定义错误页面

二.一 Tomcat 服务器配置错误页面

在以前的 Tomcat 和 JSP 时代 ,我们可以这样配置错误页面.


二.一.一 自定义 404.jsp 和 505.jsp 页面

404.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isErrorPage="true"%>
<% response.setStatus(HttpServletResponse.SC_OK);%>
404,地址错误


500.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isErrorPage="true"%>
<% response.setStatus(HttpServletResponse.SC_OK);%>
500, 服务器错误,联系管理员


二.一.二 web.xml 配置信息

在 web.xml 配置中,进行配置

<!--错误状态码指定-->  
<error-page>
  <error-code>404</error-code>
  <location>/WEB-INF/jsp/404.jsp</location>
  </error-page>
   <!--异常类型指定,也可以细化一下-->
  <error-page>
  <exception-type>java.lang.Throwable</exception-type>
  <location>/WEB-INF/jsp/500.jsp</location>
  </error-page>
   <!--错误状态码指定-->
  <error-page>
  <error-code>500</error-code>
  <location>/WEB-INF/jsp/500.jsp</location>
  </error-page>

二. 二SpringBoot 配置错误页面

根据上面的提示,我们知道,需要在 /error 目录下进行配置相关的页面信息.


配置错误页面,包括两种,一种是静态的异常页面,一种是动态的异常页面.


二.二.一 静态异常页面

在 static 目录下 创建 error 目录, 里面放置 404.html 和 500.html 页面


image.png


404.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>404异常</title>
</head>
<body>
    404异常
</body>
</html>


500.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>500 异常信息</title>
</head>
<body>
    500异常
</body>
</html>


重新运行服务器,并且访问


在访问 div (有异常的方法)

image.png


在访问 404 错误页面时

image.png


就变成了我们自定义的异常页面了.


除了指定 404 ,500 这样确切的错误状态码外,也可以使用 4xx, 5xx 这样的来统一进行接收.


二.二.二 动态错误页面

有时候常常需要将错误的信息展示出来,便于开发人员进行处理.


可以使用动态错误页面,通常放置在 templates 目录下


在 templates 目录下,创建动态错误页面

image.png



404.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>404状态码展示错误</title>
</head>
<body>
<h1>404</h1>
<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="${message}"></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>


500.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>5xx状态码展示错误</title>
</head>
<body>
<h1>5xx</h1>
<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="${message}"></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>


这五个属性信息, path, error,message,timestamp,status


是 由: org.springframework.boot.web.reactive.error.DefaultErrorAttributes 进行定义的


image.png


这个时候,再进行访问


image.png


image.png


会展示出具体的信息.


发现,动态的错误页面是生效的。


如果动态页面和静态页面同时定义了异常处理页面,


例如 classpath:/static/error/404.html 和 classpath:/templates/error/404.html 同时存在时,


默认使用动态页面。


完整的错误页面查找方式应该是这样:


发生了 404错误–>查找动态 404.html 页面–>查找静态 404.html –> 查找动态 4xx.html–>查找静态 4xx.html。


500 也一样.


发生了 500 错误–>查找动态 500.html 页面–>查找静态 500.html –> 查找动态 5xx.html–>查找静态 5xx.html。


相关文章
|
11天前
|
JSON 前端开发 Java
解决Spring MVC中No converter found for return value of type异常
在Spring MVC开发中遇到`No converter found for return value of type`异常,通常是因缺少消息转换器、返回值类型不支持或转换器优先级配置错误。解决方案包括:1) 添加对应的消息转换器,如`MappingJackson2HttpMessageConverter`;2) 自定义消息转换器并实现`HttpMessageConverter`接口,设置优先级;3) 修改返回值类型为如`ResponseEntity`的合适类型。通过这些方法可确保返回值正确转换为响应内容。
44 1
|
11天前
|
Java 数据库 Spring
Spring Boot的异常统一处理实战(包括@ExceptionHandler注解和@ControllerAdvice注解 附源码 超详细)
Spring Boot的异常统一处理实战(包括@ExceptionHandler注解和@ControllerAdvice注解 附源码 超详细)
100 0
|
11天前
|
消息中间件 监控 Java
Spring Boot中的RabbitMQ死信队列魔法:从异常到延迟,一网打尽【RabbitMQ实战 一】
Spring Boot中的RabbitMQ死信队列魔法:从异常到延迟,一网打尽【RabbitMQ实战 一】
106 0
|
11天前
|
Java
springboot全局自定义异常
springboot全局自定义异常
|
11天前
|
安全 前端开发 Java
Spring Security 自定义异常失效?从源码分析到解决方案
Spring Security 自定义异常失效?从源码分析到解决方案
|
11天前
|
Java 关系型数据库 MySQL
整合SpringBoot与MyBatis时报错时区异常
整合SpringBoot与MyBatis时报错时区异常
16 0
|
11天前
|
JSON 前端开发 Java
统一异常处理:让Spring Boot项目异常更优雅
统一异常处理:让Spring Boot项目异常更优雅
33 1
|
11天前
|
Java
SpringBoot全局异常代码复制粘贴能用
SpringBoot全局异常代码复制粘贴能用
10 0
|
11天前
|
Java
SpringBoot配置全局异常
SpringBoot配置全局异常
11 0
|
11天前
|
Java
SpringBoot全局异常@RestControllerAdvice全局异常
SpringBoot全局异常@RestControllerAdvice全局异常
13 0