当前位置: 移动技术网 > IT编程>开发语言>Java > SpringBoot自定义异常错误数据后获取不到${exception}

SpringBoot自定义异常错误数据后获取不到${exception}

2020年07月22日  | 移动技术网IT编程  | 我要评论

一. 问题背景

后台:SpringBoot2.x版本

情况:已经在application.properties配置好了server.error.include-exception=true。自定义编写一个DefaultErrorAttributes子类——MyErrorAttributes,从而能实现自定制的错误页面中 能显示 自定义的错误数据。最后其他数据能成功展示在页面了,却唯独无法获取到${exception}

核心代码如下:

package com.atguigu.springboot.component;

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

import java.util.Map;

/**
 * 给容器中加入我们自己定义的ErrorAttributes
 */
@Component
public class MyErrorAttributes extends DefaultErrorAttributes {
    /**
     * 返回值的map就是页面和json能获得的所有字段,不仅能自适应返回html数据还是json数据,
     * 还能返回xxxExceptionhandler定制的数据
     */
    @Override
    public Map<String, Object> getErrorAttributes(WebRequest webRequest, boolean includeStackTrace) {
        Map<String, Object> map = super.getErrorAttributes(webRequest, includeStackTrace);
        map.put("company", "atguigu");//这里也可以自定义数据
        //webRequest是将requestAttributes包装起来的对象,而requestAttributes是对request包装起来的对象
        // webRequest.getAttribute()获取在MyExceptionHandler中封装好的数据
        //入参0表示从request域中获取,1表示从session域中获取
        //ext是我们异常处理器MyExceptionHandler携带的数据
        Map<String, Object> ext = (Map<String, Object>) webRequest.getAttribute("ext", 0);
        map.put("ext", ext);
        return map;
    }
}

二. 解决方案

2.1 分析

上图中最关键的是调用了super,从而导致MyErrorAttributes的父类DefaultErrorAttributes中的includeException=false(因为默认为false)。

因此我们只需传入true给DefaultErrorAttributes即可。

2.2 解决

在MyErrorAttributes中通过构造器传入关键代码如下:

public MyErrorAttributes() {
    //将includeException=true传给DefaultErrorAttributes,使得能获取出exception对象
    super(true);
}

完整代码如下:

package com.atguigu.springboot.component;

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

import java.util.Map;

/**
 * 给容器中加入我们自己定义的ErrorAttributes
 */
@Component
public class MyErrorAttributes extends DefaultErrorAttributes {


    public MyErrorAttributes() {
        //将includeException=true传给DefaultErrorAttributes,使得能获取出exception对象
        super(true);
    }

    /**
     * 返回值的map就是页面和json能获得的所有字段,不仅能自适应返回html数据还是json数据,
     * 还能返回xxxExceptionhandler定制的数据
     */
    @Override
    public Map<String, Object> getErrorAttributes(WebRequest webRequest, boolean includeStackTrace) {
        Map<String, Object> map = super.getErrorAttributes(webRequest, includeStackTrace);
        map.put("company", "atguigu");//这里也可以自定义数据
        //webRequest是将requestAttributes包装起来的对象,而requestAttributes是对request包装起来的对象
        // webRequest.getAttribute()获取在MyExceptionHandler中封装好的数据
        //入参0表示从request域中获取,1表示从session域中获取
        //ext是我们异常处理器MyExceptionHandler携带的数据
        Map<String, Object> ext = (Map<String, Object>) webRequest.getAttribute("ext", 0);
        map.put("ext", ext);
        return map;
    }
}

效果:
在这里插入图片描述
有问题的伙伴可留言啊

本文地址:https://blog.csdn.net/qq_40634846/article/details/107494294

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网