springboot自定义Error数据

复杂配置

html这种配置还是不够灵活,只能定义HTML页面,无法处理JSON的定制。

Spring Boot中支持对Error信息的深度定制,接下来将从三个方面介绍深度定制:

自定义Error数据、自定义Error视图以及完全自定义。

自定义Error数据

自定义Error数据就是对返回的数据进行自定义。前面的介绍,读者已经了解到Spring Boot返回的Error信息一共有5条,分别是timestamp、status、error、message以及path。

在BasicErrorController的errorHtml方法和error方法中,都是通过getErrorAttributes方法获取Error信息的。该方法最终会调用到DefaultErrorAttributes类的getErrorAttributes方法,而DefaultErrorAttributes类是在ErrorMvcAutoConfiguration中默认提供的。

当系统没有提供ErrorAttributes时才会采用DefaultErrorAttributes。因此自定义错误提示时,只需要自己提供一个ErrorAttributes即可,而DefaultErrorAttributes是ErrorAttributes的子类,因此只需要继承DefaultErrorAttributes即可,代码如下:

pom.xml

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

 MyErrorAttribute.java

package com.shrimpking.error;


import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.WebRequest;

import java.util.Map;


/**
 * Created by IntelliJ IDEA.
 *
 * @Author : Shrimpking
 * @create 2023/6/4 18:50
 */
@Component
public class MyErrorAttribute extends DefaultErrorAttributes
{
    @Override
    public Map<String, Object> getErrorAttributes(WebRequest webRequest, ErrorAttributeOptions options)
    {
        Map<String, Object> errorAttributes = super.getErrorAttributes(webRequest, options);
        errorAttributes.put("customMsg","出错啦");
        errorAttributes.remove("error");
        return errorAttributes;
    }
}

4xx.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>4xx</title>
</head>
<body>
<table border="1">
    <tr>
        <td>timestamp</td>
        <td th:text="${timestamp}"></td>
    </tr>
    <tr>
        <td>status</td>
        <td th:text="${status}"></td>
    </tr>
    <tr>
        <td>error</td>
        <td th:text="${error}"></td>
    </tr>
    <tr>
        <td>message</td>
        <td th:text="${message}"></td>
    </tr>
    <tr>
        <td>path</td>
        <td th:text="#{path}"></td>
    </tr>
    <tr>
        <td>customMsg</td>
        <td th:text="${customMsg}"></td>
    </tr>
</table>
</body>
</html>

代码解释:

• 自定义MyErrorAttribute继承自DefaultErrorAttributes,重写DefaultErrorAttributes中的getErrorAttributes方法。

MyErrorAttribute类添加@Component注解,该类将被注册到Spring容器中。

• 通过super.getErrorAttributes获取Spring Boot默认提供的错误信息,然后在此基础上添加Error信息或者移除Error信息。

此时,当系统抛出异常时,错误信息将被修改,以动态页面模板4xx.html为例,添加了custommsg属性,此时访问一个不存在的路径,就能看到自定义的Error信息,并且可以看到默认的error被移除了,如果通过Postman等工具来发起这个请求,那么返回的JSON数据中也是如此.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

虾米大王

有你的支持,我会更有动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值